I'm working on an upgrade of an application to use Symfony 3 and PHP 7. I keep getting this error. I'm not sure where it stems from or how to fix it.
Symfony\Component\Debug\ExceptionHandler::handle() must be an instance of Exception
TL;DR: A PHP error is triggered somewhere in your code. In PHP 5, it was triggered as a simple PHP error; in PHP 7 it's thrown as an Error
that is now passed into Symfony's exception handler.
This error has probably always been in your application and was swallowed due to error_reporting
or display_errors
settings.
The error handling mechanism changed in PHP 7. Errors are now thrown as instances of the Error
class and can be caught by an exception handler. See the documentation for more information:
PHP 7 changes how most errors are reported by PHP. Instead of reporting errors through the traditional error reporting mechanism used by PHP 5, most errors are now reported by throwing Error exceptions.
As with normal exceptions, these
Error
exceptions will bubble up until they reach the first matching catch block. If there are no matching blocks, then any default exception handler installed withset_exception_handler()
will be called, and if there is no default exception handler, then the exception will be converted to a fatal error and will be handled like a traditional error.
However, note that the Error
class does not extend the Exception
class (they both implement the Throwable
interface, though).
Since the Symfony exception handler was registered as an exception handler using the set_exception_handler
method, this handler will be invoked with all kinds of uncaught Error
instances thrown around in your code, but cannot handle them as it expects an Exception
per its type hint.
For the time being, you could solve this by implementing your own ExceptionHandler (that you can then use instead of Symfony's) that implements a handle(Throwable $error)
function, allowing you to also catch PHP Errors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With