Patrick
Patrick

Reputation: 4905

Avoiding specific PHP Fatal error: Uncaught exception to allow script to continue running

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?

Upvotes: 0

Views: 2884

Answers (3)

berty
berty

Reputation: 319

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
}

Upvotes: 1

Tyler Carter
Tyler Carter

Reputation: 61577

// run some code

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

// keep running script.

Upvotes: 2

Galen
Galen

Reputation: 30170

catch the exception!!!

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

Upvotes: 2

Related Questions