Jump to content

Error handling without specific try/catch blocks


mottwsc

Recommended Posts

Anytime an error/exception occurs in my php 5 code, I'd like to have the specific error written to a table along with other info (user, date, etc.) and have a more user-friendly message sent to the user.  These user-friendly messages could be fairly generic (maybe 10 or 20 different ones in total) or they could be more specific if that doesn't require a lot more coding.

 

I'm somewhat familiar with the catch/try methodology, but it seems like I would have to put in a lot of extra code all through each program to achieve this.  Is there a way to have exceptions/errors handled without all of those specific try/catch blocks?

 

Thanks.

 

There are a few ways:

 

1. Create a fallback try/catch.

 

try {
    //application code
} catch (Exception $e) {
    //catches any uncaught exception's
}

 

2. Use set_error_handler() & set_exception_handler()

 

set_error_handler('error_handler');
set_exception_handler('exception_handler');

function error_handler($errno, $errstr, $errfile = '', $errline = 0, array $errcontext = array()) {}
function exception_handler($exception) {}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.