NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP 5 Exception Expertise Needed

Adam Maccabee Trachtenberg adam at trachtenberg.com
Sat Mar 19 11:49:13 EST 2005


On Sat, 19 Mar 2005, Joseph Crawford wrote:

> If an exceptions is thrown by PHP such as a fatal error or what not, i
> cannot use my Display method.  Is there an easy way to rethrow an
> exception as a certain type?  I would like to rethrow all exceptions
> as SvEx type so that i can use my display method to get it to show a
> nicely formatted error to go wtih the site's design, here is what i
> have so far

To catch all exceptions without wrapping code inside try/catch blocks, do this:

function my_exception_handler($e) {
    throw new SvEx($e->getMessage(), $e->getCode());
}

set_exception_handler('my_exception_handler');

Although, it may be better to aggregate the original Exception inside
your custom one, as I believe the debugging information will be
disrupted. (Such as the line number and stack trace.)

However, the good practice fairy says it's better to really wrap stuff
inside try/catch, so you know exactly what you're doing and when
you're doing it. (I guess you could always just make a master
try/catch around the main control flow.)

-adam

PS: Upgrading to PHP5: Chapter 7. Error Handling and Debugging:
Setting a Custom Exception Handler

-- 
adam at trachtenberg.com | http://www.trachtenberg.com
author of o'reilly's "upgrading to php 5" and "php cookbook"
avoid the holiday rush, buy your copies today!



More information about the talk mailing list