Forum Moderators: coopster
For instance...
try {
$myvar = 5 / 0;
throw new Exception('Stop here!');
} catch (Exception $e) {
echo 'EXCEPTION TRAPPED: '.$e->getMessage();
} Is this as expected?
function divide($a, $b) {
if(!$b)
throw new Exception("Division by zero");
else return $a/$b;
}
So basically, unless it's user-defined, I'm pretty sure PHP doesn't throw exceptions on it's own, otherwise you'd get a lot of fatal errors with uncaught exceptions.
try-catch blocks also won't catch errors you throw via trigger_error. As has been mentioned, you could use set_error_handler to throw exceptions yourself. Specifically, see the ErrorException class [us.php.net] which provides an example of how to do this.