If you select an exception without a try / catch block, a fatal error will occur. An error message appears indicating the reason for this avoidable circumstance: "Fatal error: unclean exception", and your program terminates without executing the remaining code; see here . Manual mentions that this is a common result:
if the handler was not defined using set_exception_handler ()
Setting the exception handler allows you to avoid the "Fatal error" message and allows you to handle the exception as you wish, but after the handler stops the program; see here .
The try / catch block provides code a opportunity to try to execute. If an exception occurs, you can safely handle it by catching it, which will prevent the program from abruptly stopping as follows:
<?php try { $myvar = null; if (!isset($myvar)) { throw new Exception("unset variable"); } } catch (Exception $e) { echo $e->getMessage(); } echo "\nStill carrying on and on ...\n";
Watch the demo
source share