Php/docs/exception.getprevious

提供:Dev Guides
< Php
移動先:案内検索

Exception::getPrevious

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

Exception::getPrevious前の例外を返す


説明

final public Exception::getPrevious ( ) : Exception

前に発生した例外 (Exception::__construct() の 3 番目の引数) を返します。


パラメータ

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


返り値

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


例1 Exception::getPrevious() の例

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


<?phpclass 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]

参考