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.

 

Link to comment
Share on other sites

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) {}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.