Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / PHP

Errori ed eccezioni

Throwable comprende Exception ed Error, incluso TypeError.

Versione: PHP 8.4; repository runtime, production runtime not attested

Sintassi

catch (Throwable $failure) { /* handle */ }

Throwable comprende Exception ed Error, incluso TypeError. Leggere una chiave assente genera un avviso anziché un’eccezione. Un gestore può trasformare un avviso scelto in ErrorException. Ripristinare poi il gestore precedente.

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

Esempi

<?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();}

Risultato

ErrorException warning

Fonti