Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Throwable

Use the shared interface of Error and Exception to handle both object hierarchies.

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

Syntax

catch (Throwable $failure) { /* inspect cause */ }

Use the shared interface of Error and Exception to handle both object hierarchies. It provides the message, code, previous cause and trace. A user-defined class cannot implement Throwable directly without inheriting from Error or Exception. Catch the narrower type when only one kind is expected.

$types=[];
foreach([new LogicException("L"),new TypeError("T")] as $problem){ try{throw $problem;}catch(Throwable $e){$types[]=[get_class($e),$e->getMessage()];} }
echo json_encode($types);
[["LogicException","L"],["TypeError","T"]]

Examples

<?php
$types=[];
foreach([new LogicException("L"),new TypeError("T")] as $problem){ try{throw $problem;}catch(Throwable $e){$types[]=[get_class($e),$e->getMessage()];} }
echo json_encode($types);

Result

[["LogicException","L"],["TypeError","T"]]

Sources