# Engine Exceptions

> Section in “New and Extended Functionality › Error Handling” 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/engine-exceptions/>

Thanks to reclassification and internal engine changes, many previously fatal errors can now be handled within the user land code and do not leave the runtime in an undefined state. To allow this, PHP 7 uses a new type of exception. PHP 5 only had fatal or recoverable fatal errors. To not interfere with existing application behavior and to generally have a clear distinction from classic exceptions the new engine exceptions do not extend the existing `Exception` base class. Instead, they extend a new base class named `Error`. This allows for a very explicit distinction when catching and handling them:

``` php
try {
    // ...
} catch (Error $error) {
    // ...
} catch (Exception $exception) {
    // ...
}
```

Both `Exception` as well as `Error` implement the new internal interface `Throwable`, which defines the same API as the existing `Exception` class – with the minor difference of the return type for the method `getPrevious()` now being `Throwable` rather than `Exception`:

``` php
interface Throwable
{
    public function getMessage(): string;
    public function getCode(): int;
    public function getFile(): string;
    public function getLine(): int;
    public function getTrace(): array;
    public function getTraceAsString(): string;
    public function getPrevious(): Throwable;
    public function __toString(): string;
}
```

Thanks to this new hierarchy, both `Exception` as well as `Error` can also conveniently be caught and handled with one `catch` block:

``` php
try {
  // ...
} catch (Throwable $throwable) {
  // ...
}
```

PHP 5 does not know about the `Throwable` and `Error` types but will also not trigger any autoload for them when they are used within a `catch` block. That means the non-matching lines will simply be ignored. This approach can also be used if you want to update your code to PHP 7 while maintaining compatibility to older versions.

If you want to implement your own error or exception classes keep in mind that the `Throwable` interface is limited to internal use and userland code cannot directly implement it. While this limitation may be removed in future versions of PHP – rumor has it that this is already being discussed – at the time of this writing all userland error or exception classes must extend from either `Error` or `Exception`.

---

Previous: [Error Handling](https://php7explained.com/new-and-extended-functionality/error-handling/index.html.md) | Next: [Parse Errors](https://php7explained.com/new-and-extended-functionality/error-handling/parse-errors/index.html.md)
