Exception::getPrevious

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

Exception::getPreviousRetorna o Throwable anterior

Descrição

finalpublicException::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

To Top