Exception::getPrevious

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

Exception::getPrevious前の例外(Throwable)を返す

説明

finalpublicException::getPrevious(): ?Throwable

前に発生した Throwable (Exception::__construct() に渡された 3 番目の引数) を返します。

パラメータ

この関数にはパラメータはありません。

戻り値

前に発生した Throwable、あるいはそれが存在しない場合は null を返します。

例1 Exception::getPrevious() の例

例外トレースをループし、表示します。

<?php
class MyCustomException extends Exception {}

function
doStuff() {
try {
throw new
InvalidArgumentException("You are doing it wrong!", 112);
} catch(
Exception $e) {
throw new
MyCustomException("Something happened", 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());
}
?>

上の例の出力は、 たとえば以下のようになります。

/home/bjori/ex.php:8 Something happened (911) [MyCustomException] /home/bjori/ex.php:6 You are doing it wrong! (112) [InvalidArgumentException]

参考

To Top