PHP 8.4.0 RC3 available for testing

Exception::getPrevious

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

Exception::getPreviousRetorna o Throwable anterior

Descrição

final public Exception::getPrevious(): ?Throwable

Retorna o Throwable anterior (o terceiro valor passado no construtor Exception::__construct()).

Parâmetros

Esta função não possui parâmetros.

Valor Retornado

Retorna a Throwable anterior se disponível, ou senão null.

Exemplos

Exemplo #1 Exception::getPrevious() example

Iterando e imprimindo a pilha de erro.

<?php
class MyCustomException extends Exception {}

function
doStuff() {
try {
throw new
InvalidArgumentException("Primeiro erro lançado.", 112);
} catch(
Exception $e) {
throw new
MyCustomException("Segundo erro lançado.", 911, $e);
}
}


try {
doStuff();
} catch(
Exception $e) {
do {
printf("%s:%d %s (%d) [%s]\n", $e->getFile(), $e->getLine(), $e->getMessage(), $e->getCode(), get_class($e));
} while(
$e = $e->getPrevious());
}
?>

O exemplo acima produzirá algo semelhante a:

/home/bjori/ex.php:8 Segundo erro lançado. (911) [MyCustomException]
/home/bjori/ex.php:6 Primeiro erro lançado. (112) [InvalidArgumentException]

Veja Também

adicione uma nota

Notas Enviadas por Usuários (em inglês) 1 note

up
11
harry at upmind dot com
5 years ago
/**
* Gets sequential array of all previously-chained errors
*
* @param Throwable $error
*
* @return Throwable[]
*/
function getChain(Throwable $error) : array
{
$chain = [];

do {
$chain[] = $error;
} while ($error = $error->getPrevious());

return $chain;
}
To Top