質問

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 ?

役に立ちましたか?

解決

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.

他のヒント

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();
  }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top