Page 1 of 1

Error handling without specific try/catch blocks

Posted: Sat Aug 01, 2009 10:07 am
by mottwsc
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 try/catch 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.

Re: Error handling without specific try/catch blocks

Posted: Sat Aug 01, 2009 10:11 am
by jackpf
You mean like set_error_handler()?

Re: Error handling without specific try/catch blocks

Posted: Sat Aug 01, 2009 10:33 am
by mottwsc
Thanks, this may well do it. It looks like the custom function only has to be established once be script, and then it overrides the normal error handling, except that certain error conditions (E_ERROR, E_PARSE, etc.) cannot be caught, so they will display to the user.

I'll look into this further. Beyond the PHP manual, if anyone knows about a good tutorial for this, please say so.

Thank you.

Re: Error handling without specific try/catch blocks

Posted: Sat Aug 01, 2009 10:36 am
by jackpf
Well, it does catch parse errors.

Only, it won't catch parse errors in the script it's set in obviously, because there's a parse error so that script won't execute, so your custom error function will not have been set. But it will catch parse errors in other scripts. If you get me.... :P

Re: Error handling without specific try/catch blocks

Posted: Sat Aug 01, 2009 10:53 am
by mottwsc
Yes, I do understand. Thanks.