...

View Full Version : Throwing and Catching Exceptions in PHP



Ahlahn
05-31-2011, 03:05 AM
Lets say I have the following code

$test = preg_match_all($regular_expression, $string, $blah);

If my regular expression is incorrect, it will display a "missing delimiter" error or a "out of range" error. How do I use exceptions in PHP so that I can catch the exception and display a unique message without seeing the error message?

Fou-Lu
05-31-2011, 07:04 AM
Errors can be overridden to throw exceptions instead of triggering errors. You can wrap a custom function to throw an ErrorException instead:


function errorExceptionHandler($errno, $errstr, $errfile, $errline)
{
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}

set_error_handler('errorExceptionHandler');


Error handling cannot be done on any core or fatal error. If there is a problem with the interpretation of the code, there is no way to override an error handler.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum