0

i call an php pgm per cronjob at different times. the pgm includes many php-files. each file sends or gets data from partners.

How can i handle errors in one includes pgm.

at the time, one ftp-connection in an included pgm fails so the complete script crushes. how can i handle this ?

j0k
  • 22,600
  • 28
  • 79
  • 90
user1121575
  • 390
  • 1
  • 4
  • 19
  • what do you mean with pgm (programme?), for the error handling you can use try catch blocks: http://php.net/manual/en/language.exceptions.php – Jurgo Aug 14 '12 at 12:47

2 Answers2

0

You should wrap code, which is possible to crash, into try/catch construction. This will throw exeption, but the script will continue to work. More here.

tijs
  • 566
  • 4
  • 12
  • Native php functions rearly throws exceptions. So wrapping code with try/catch block seldom helps out. – xCander Aug 14 '12 at 13:06
0

Need to know more about you code inorder to give you definite answer.

In general php errors isn't catchable unless you define your own error handler from which you throw exceptions your self. Using the code below makes most runtime errors catchable (as long as they arent considered fatal)

error_reporing(E_ALL);

set_error_handler(function($errno, $errstr, $errfile, $errline) {
  if($errno == E_STRICT || $errno == E_DEPRECATED) {
    return true;
  }
  throw new RuntimeException('Triggered error (code '.$errno.') with message "'.$errstr.'"');
});

Btw, You could also define your own exception handler to display triggered errors with a full stack trace when an exception isn't catched.

Notice! I would not suggest that you add this code to a production website without rigorous testing first, making sure everything still works as expected.

Edit:

I have no idea what your code looks like, but I guess you can do something like:

require 'error-handler.php'; // where you have your error handler (the code seen above)

$files_to_include = array(
  'some-file.php',
  'some-other-file.php',
  ...
);

foreach($files_to_include as $file) {
  try {
    include $file;
  }
  catch(Exception $e) {
    echo "$file failed\nMessage: ".$e->getMessage()."\nTrace:\n".$e->getTraceAsString();
  }
}
xCander
  • 1,338
  • 8
  • 16
  • i include 10 php files. each file opens a ftp connection and loads data from partners. if a partner changes the ftp-connection-settings without telling me, the whole script crashes with an login-incorrect error. – user1121575 Aug 15 '12 at 08:04