views:

275

answers:

3

Hi all,

I would like to make it so that my scripts can continue to run even if there's a CERTAIN fatal error. Right now I can get this fatal error: PHP Fatal error: Uncaught exception 'MongoConnectionException' with message blah blah.

How do we catch this specific error, log it, but allow the script to continue to run? Anyone has idea regarding this?

+2  A: 
// run some code

try{
   // run code that throws the exception
}
catch(MongoConnectionException $e)
{
    error_log($e->getMessage());
    // or other logging capabilities
}

// keep running script.
Chacha102
Just to add... One really easy way to log it is to use the php function error_log like so:error_log($e->getMessage());
Eric Butera
Added to the answer. Thnx @Eric.
Chacha102
+2  A: 

catch the exception!!!

http://php.net/manual/en/language.exceptions.php

Galen
+1  A: 

More generally on this subject, a little caution is required, as standard PHP fatal errors are not automatically converted into exceptions, this modified a little from the manual should go some way to mitigate this.

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");

try {
    /* Trigger exception */
    strpos();
}
catch (ErrorException $e) {
    // deal with the error
}
berty