Exception::getPrevious
(PHP 5 >= 5.3.0, PHP 7, PHP 8)
Exception::getPrevious — 前の例外を返す
説明
final public Exception::getPrevious ( ) : Exception
前に発生した例外 (Exception::__construct() の 3 番目の引数) を返します。
パラメータ
この関数にはパラメータはありません。
例
例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]