Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP 7 Error Handling

PHP 7 Error Handling

PHP 7 New Features PHP 7 New Features

PHP 7 changed the way most errors are reported. Unlike the traditional error reporting mechanism of PHP 5, now most errors are treated as Error exceptions.

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 via set_exception_handler()) is called to handle it. If no exception handler has been registered, it is handled in the traditional way: reported as a fatal error (Fatal Error).

The Error class does not extend the Exception class, so code like catch (Exception $e) { ... } cannot catch an Error. You can use code like catch (Error $e) { ... }, or capture Error by registering an exception handler (set_exception_handler()).

Error Exception Hierarchy

- **ArithmeticError** - **AssertionError** - **DivisionByZeroError** - **ParseError** - **TypeError** - ...

Example

Example

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 7 New Features