Error handling without specific try/catch blocks

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mottwsc
Forum Commoner
Posts: 55
Joined: Sun Dec 23, 2007 8:01 pm

Error handling without specific try/catch blocks

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Error handling without specific try/catch blocks

Post by jackpf »

You mean like set_error_handler()?
mottwsc
Forum Commoner
Posts: 55
Joined: Sun Dec 23, 2007 8:01 pm

Re: Error handling without specific try/catch blocks

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Error handling without specific try/catch blocks

Post 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
Last edited by jackpf on Sat Aug 01, 2009 10:57 am, edited 1 time in total.
mottwsc
Forum Commoner
Posts: 55
Joined: Sun Dec 23, 2007 8:01 pm

Re: Error handling without specific try/catch blocks

Post by mottwsc »

Yes, I do understand. Thanks.
Post Reply