The set_exception_handler() is an inbuilt function of PHP and is used to sets a user-defined exception handler function. It will returns the name of the previously defined exception handler, or NULL
on error. If no previous handler was defined, NULL
is also returned.
Example #1 set_exception_handler() function example
1 2 3 4 5 6 7 8 9 10 |
<?php function exception_handler($exception) { echo "Uncaught exception: " , $exception->getMessage(), "\n"; } set_exception_handler('exception_handler'); throw new Exception('Uncaught Exception'); echo "Not Executed\n"; ?> |
Example #2 set_exception_handler() function example
If you want a class instance to handle the exception, this is how you do it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php class example { public function __construct() { @set_exception_handler(array($this, 'exception_handler')); throw new Exception('DOH!!'); } public function exception_handler($exception) { print "Exception Caught: ". $exception->getMessage() ."\n"; } } $example = new example; ?> |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.