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

CKTIS / PHP

Exception

Descreve uma condição excecional com mensagem, código inteiro e causa Throwable opcional. throw interrompe o fluxo até um catch correspondente.

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

Sintaxe

new Exception($message, $code, $previous)

Descreve uma condição excecional com mensagem, código inteiro e causa Throwable opcional. throw interrompe o fluxo até um catch correspondente. Ao traduzir o erro passe o original como previous. Exception por si só não inclui Error. O método Exception::getMessage devolve o texto desta exceção, aqui «job». A mensagem da causa «source» lê-se separadamente com $e->getPrevious()?->getMessage() se existir uma exceção anterior.

$cause=new RuntimeException("source",8);
try { throw new Exception("job",12,$cause); }
catch(Exception $e){echo json_encode([$e->getMessage(),$e->getCode(),$e->getPrevious()->getMessage()]);}
["job",12,"source"]

Exemplos

<?php
$cause=new RuntimeException("source",8);
try { throw new Exception("job",12,$cause); }
catch(Exception $e){echo json_encode([$e->getMessage(),$e->getCode(),$e->getPrevious()->getMessage()]);}

Resultado

["job",12,"source"]

Fontes