# Error Handling

> Section in “New and Extended Functionality” of the book [PHP 7 Explained](https://php7explained.com/) by Sebastian Bergmann and Stefan Priebsch, published by thePHP.cc. The book covers PHP 7.0 to 7.4 and was last updated in November 2020. HTML version: <https://php7explained.com/new-and-extended-functionality/error-handling/>

Sooner or later every application has to deal with error conditions and programming mistakes. Unless something is considered a really serious problem, PHP simply emits a warning message, and then goes on as if nothing happened. Back in the days, when PHP code was embedded into HTML documents, this lax way of handling errors was considered a feature, an explicit design choice when creating PHP as a language. In a modern, object-oriented PHP application, where an explicit handling of all error conditions is preferred, such behavior is rather counterproductive.

To fix this and to handle errors in an object-oriented way with PHP 5.6 and earlier a custom error handler had to be registered, converting errors into exceptions. For example by using the built-in `ErrorException` class:

``` php
set_error_handler(
    function($errno, $errstr, $errfile, $errline, array $errcontext)
    {
        throw new ErrorException(
            $errstr, 0, $errno, $errfile, $errline
        );
    }
);
```

The error handler shown above will transform every error into an exception which can then be conveniently caught and handled by the calling code.

The fifth parameter `$errcontext` – which contains a list of all local variables available at the error location – is not used and could be omitted in the function declaration. Starting with PHP 7.2, it is deprecated and should no longer be used. In contrast to other deprecated functionality, though, this will not trigger the usual notice as this would cause an endless chain of invocations of the registered error handler.

Sadly, this approach does not work for all error types. Some error types are considered more serious and do not trigger the registered error handler. While PHP 5.6 does not provide a way to recover from things like parsing errors, registering a shutdown handler at least provided a means to a controlled shutdown.

Luckily, with PHP 7 all this changed fundamentally.

## In this section

- [Engine Exceptions](https://php7explained.com/new-and-extended-functionality/error-handling/engine-exceptions/index.html.md): PHP 7 turns many fatal errors into engine exceptions that extend the new Error base class, which shares the new Throwable interface with Exception.
- [Parse Errors](https://php7explained.com/new-and-extended-functionality/error-handling/parse-errors/index.html.md): Since PHP 7, a syntax error in an included or required file throws a ParseError that can be caught with try and catch, so no shutdown handler is needed.
- [Parse Errors in evaluated Code](https://php7explained.com/new-and-extended-functionality/error-handling/parse-errors-in-evaluated-code/index.html.md): In PHP 7, a parse error in code run via eval() throws a ParseError, so statements following the eval() call are no longer reached as they were in PHP 5.
- [Recoverable Fatal Errors](https://php7explained.com/new-and-extended-functionality/error-handling/recoverable-fatal-errors/index.html.md): Recoverable fatal errors, such as an argument that does not match the declared parameter type, throw a TypeError in PHP 7 and can be caught as Error.
- [Method Call on Non-Object](https://php7explained.com/new-and-extended-functionality/error-handling/method-call-on-non-object/index.html.md): Calling a method on a string or another non-object value throws an Error in PHP 7 instead of a fatal error, so it can be handled with a catch block.
- [CompileError](https://php7explained.com/new-and-extended-functionality/error-handling/compileerror/index.html.md): PHP 7.3 introduces CompileError, which extends Error and is the parent of ParseError, thrown for compilation errors in token_get_all() in TOKEN_PARSE mode.
- [Errors in Extensions](https://php7explained.com/new-and-extended-functionality/error-handling/errors-in-extensions/index.html.md): Since PHP 7.1, core extensions such as date, dom, mbstring, mysqli, reflection, session, spl, and zip throw Error instead of fatal or recoverable errors.
- [Constructors of Internal Classes](https://php7explained.com/new-and-extended-functionality/error-handling/constructors-of-internal-classes/index.html.md): As of PHP 7, constructors of internal classes such as finfo, PDO, NumberFormatter, SplFixedArray, and Phar throw an exception or error for invalid input.
- [Better syslog support with error_log](https://php7explained.com/new-and-extended-functionality/error-handling/better-syslog-support-with-error_log/index.html.md): As of PHP 7.1, PHP error levels such as NOTICE or WARNING are mapped to their syslog counterparts when errors are logged to syslog, which eases filtering.
- [error_clear_last()](https://php7explained.com/new-and-extended-functionality/error-handling/error_clear_last/index.html.md): PHP 7 adds error_clear_last() to reset the last error returned by error_get_last(), which could not be cleared in PHP 5 once it had been handled.
- [Changed E_STRICT Notices](https://php7explained.com/new-and-extended-functionality/error-handling/changed-e_strict-notices/index.html.md): In PHP 7, all cases that triggered an E_STRICT notice were reclassified to other error types or removed; the E_STRICT constant remains but has no effect.
- [__toString() and Exceptions](https://php7explained.com/new-and-extended-functionality/error-handling/tostring-and-exceptions/index.html.md): Before PHP 7.4, raising an exception inside a __toString() method was not allowed and caused a fatal error; PHP 7.4 and later lift this limitation.
- [Chained Autoloaders and Exceptions](https://php7explained.com/new-and-extended-functionality/error-handling/chained-autoloaders-and-exceptions/index.html.md): Since PHP 7.3, PHP stops calling functions registered with spl_autoload_register() as soon as one raises an exception instead of chaining the exceptions.
- [Stack Traces and Arguments](https://php7explained.com/new-and-extended-functionality/error-handling/stack-traces-and-arguments/index.html.md): The zend.exception_ignore_args configuration option introduced in PHP 7.4 controls whether exception stack traces include function and method arguments.
- [JSON_THROW_ON_ERROR](https://php7explained.com/new-and-extended-functionality/error-handling/json_throw_on_error/index.html.md): With the JSON_THROW_ON_ERROR option of PHP 7.3, json_encode() and json_decode() throw a JsonException instead of returning false or NULL on errors.
- [Handling Multiple Exception Types with one catch Statement](https://php7explained.com/new-and-extended-functionality/error-handling/handling-multiple-exception-types-with-one-catch-statement/index.html.md): Starting with PHP 7.1, one catch statement can handle several exception types, as in catch (SomeDomainException|PersistenceException $e).

---

Previous: [New and Extended Functionality](https://php7explained.com/new-and-extended-functionality/index.html.md) | Next: [Engine Exceptions](https://php7explained.com/new-and-extended-functionality/error-handling/engine-exceptions/index.html.md)
