View Full Version : Throwing and Catching Exceptions in PHP

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?

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);


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.