CKTIS / PHP
Errores y excepciones
Throwable engloba Exception y Error, incluido TypeError.
Versión: PHP 8.4; repository runtime, production runtime not attested
Sintaxis
catch (Throwable $failure) { /* handle */ }Throwable engloba Exception y Error, incluido TypeError. Leer una clave ausente genera un aviso en vez de una excepción. Un gestor puede convertir un aviso seleccionado en ErrorException. Restaure después el gestor anterior.
set_error_handler(function($level,$message){throw new ErrorException($message,0,$level);},E_WARNING);
try {$row=[]; $x=$row["absent"];} catch(Throwable $e){echo get_class($e)," ",$e->getSeverity()===E_WARNING?"warning":"other";} finally {restore_error_handler();}
ErrorException warning
Ejemplos
<?php
set_error_handler(function($level,$message){throw new ErrorException($message,0,$level);},E_WARNING);
try {$row=[]; $x=$row["absent"];} catch(Throwable $e){echo get_class($e)," ",$e->getSeverity()===E_WARNING?"warning":"other";} finally {restore_error_handler();}
Resultado
ErrorException warning