Capturing and Displaying PHP Errors and Exceptions


Set a custom error handler and exception handler.

Source Code

set_error_handler(function ($severity, $message, $file, $line) {
    throw new ErrorException($message, 0, $severity, $file, $line);
});

set_exception_handler(function ($exception) {
    echo "Uncaught exception: " , $exception->getMessage(), "n";
});

try {
    // Code that may throw an exception
} catch (Exception $e) {
    echo "Caught exception: " . $e->getMessage();
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments