YouTip LogoYouTip

Php Error Handling

# PHP 7 Error Handling [![Image 4: PHP 7 New Features](#) PHP 7 New Features](#) PHP 7 changed the way most errors are reported. Unlike PHP 5's traditional error reporting mechanism, most errors are now thrown as **Error** exceptions. This Error exception can be caught by a try / catch block just like a normal exception. If there is no matching try / catch block, the exception handler function (registered by set_exception_handler()) is called to handle it. If no exception handler function has been registered, it is handled in the traditional way: reported as a Fatal Error. The Error class is not extended from the Exception class, so code like catch (Exception $e) { ... } cannot catch an Error. You can use code like catch (Error $e) { ... } or register an exception handler function (set_exception_handler()) to catch an Error. ### Error Exception Hierarchy * **Error** * **ArithmeticError** * **AssertionError** * **DivisionByZeroError** * **ParseError** * **TypeError** * Exception * ... !(#) ### Example ## Example ```php n % 0; return $value; } catch (DivisionByZeroError $e) { return $e->getMessage(); } } } $mathOperationsObj = new MathOperations(); print($mathOperationsObj->doOperation()); ?> The output of the above program is: Modulo by zero * * PHP 7 New Features](#)
← Php Session OptionsPhp Error Handling β†’