« Exception::getMessage
Exception::getCode »

Exception::getPrevious

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

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

説明

final public function Exception::getPrevious(): ?Throwable

前に発生した Throwable (Exception::__construct() に渡された第三パラメータ) を返します。

パラメータ

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

戻り値

前に発生した 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]

参考

関連キーワード:
Exception   Throwable   getPrevious   MyCustomException   getMessage   getCode   doStuff   throw   InvalidArgumentException   doing   wrong   catch   Something   happened   home   bjori   final   construct   トレース   ループ   extends   printf   getFile   getLine   while
関連する関数・ページ:
error.getprevious   throwable.getprevious   yaf-exception.getprevious   class.yaf-exception   class.ui-exception-invalidargumentexception   class.invalidargumentexception