Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / PHP

Erros e exceções

Throwable abrange Exception e Error, incluindo TypeError.

Versão: PHP 8.4; repository runtime, production runtime not attested

Sintaxe

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

Throwable abrange Exception e Error, incluindo TypeError. Ler uma chave ausente gera um aviso em vez de uma exceção. Um tratador pode converter um aviso selecionado em ErrorException. Reponha depois o tratador 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

Exemplos

<?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

Fontes