# `__toString()` and 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/tostring-and-exceptions/>

Prior to PHP 7.4, raising an exception during the execution of a `__toString()` method was not allowed. This was due to the technical limitation that not all parts of the PHP runtime’s codebase were able to deal with exceptions when an automatic object-to-string conversion needs to be performed.

``` php
class C
{
    public function __toString(): string
    {
        throw new Exception;
    }
}

print new C;
```

Executing the code shown above with PHP 7.3 and earlier resulted in the error shown below:

    Fatal error: Method C::__toString() must not throw an exception,
    caught Exception: in ...

Executing the same code with PHP 7.4 and later prints the output shown below:

    Fatal error: Uncaught Exception in ...

---

Previous: [Changed E_STRICT Notices](https://php7explained.com/new-and-extended-functionality/error-handling/changed-e_strict-notices/index.html.md) | Next: [Chained Autoloaders and Exceptions](https://php7explained.com/new-and-extended-functionality/error-handling/chained-autoloaders-and-exceptions/index.html.md)
