Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Errors and exceptions

Throwable covers Exception and Error objects, including TypeError.

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

Syntax

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

Throwable covers Exception and Error objects, including TypeError. Ordinary warnings such as reading an absent array key are reported through the error handler instead of being thrown. A handler may deliberately turn a selected warning into ErrorException. Restore the previous handler when the operation finishes.

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

Examples

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

Result

ErrorException warning

Sources