You can catch both exceptions and errors by catching(Throwable)
PHP 7 cambia la mayoría de los errores notificados por PHP. En lugar de notificar errores a través del mecanismo de notificación de errores tradicional de PHP 5, la mayoría de los errores ahora son notificados lanzando excepciontes Error.
Al igual que las excepciones normales, las excepciones Error se propagarán hasta alcanzar el primer bloque catch coincidente. Si no hay bloques coincidentes, será invocado cualquier manejador de excepciones predeterminado instalado con set_exception_handler(), y si no hubiera ningún manejador de excepciones predeterminado, la excepción será convertida en un error fatal y será manejada como un error tradicional.
Debido a que la jerarquía de Error no hereda de
Exception, el código que emplee bloques
catch (Exception $e) { ... }
para manejar excepciones no
capturadas en PHP 5 encontrará que estos Errores no
son capturados por dichos bloques. Se requiere, por tanto, un bloque catch (Error $e) { ... }
o un manejador set_exception_handler().
You can catch both exceptions and errors by catching(Throwable)
php 7.1
try {
// Code that may throw an Exception or ArithmeticError.
} catch (ArithmeticError | Exception $e) {
// pass
}
Throwable does not work on PHP 5.x.
To catch both exceptions and errors in PHP 5.x and 7, add a catch block for Exception AFTER catching Throwable first.
Once PHP 5.x support is no longer needed, the block catching Exception can be removed.
try
{
// Code that may throw an Exception or Error.
}
catch (Throwable $t)
{
// Executed only in PHP 7, will not match in PHP 5
}
catch (Exception $e)
{
// Executed only in PHP 5, will not be reached in PHP 7
}