CKTIS / PHP
Throwable
Interface comum a Error e Exception para tratar ambas as hierarquias.
Versão: PHP 8.4; repository runtime, production runtime not attested
Sintaxe
catch (Throwable $failure) { /* inspect cause */ }Interface comum a Error e Exception para tratar ambas as hierarquias. Fornece mensagem, código, causa anterior e rastreio. Uma classe própria tem de herdar de Error ou Exception para ser Throwable. Use catch específico se só esperar um tipo.
$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"]]
Exemplos
<?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);
Resultado
[["LogicException","L"],["TypeError","T"]]