PHP 7 Error Handling
PHP 7 changed the way most errors are reported. Unlike the traditional error reporting mechanism of PHP 5, most errors are now thrown asErrorexceptions.
This Error exception can be caught by try / catch blocks 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 Error. You can use code like catch (Error $e) { ... }, or register an exception handler function (set_exception_handler()) to catch Error.
Error exception hierarchy
-
Error
- ArithmeticError
- AssertionError
- DivisionByZeroError
- ParseError
- TypeError
-
Exception
- ...

Example
Example
class MathOperations
{
protected $n = 10;
// Modulo operation, divisor is 0, throws an exception
public function doOperation(): string
{
try {
$value = $this->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
Other extensions
PHP 7 New Features