(PHP 7, PHP 8)
Error::getPrevious — 返回前一个 Throwable
此函数没有参数。
示例 #1 Error::getPrevious() 示例
循环遍历并打印出错误跟踪。
<?php
class MyCustomError extends Error {}
function doStuff() {
try {
throw new InvalidArgumentError("You are doing it wrong!", 112);
} catch(Error $e) {
throw new MyCustomError("Something happened", 911, $e);
}
}
try {
doStuff();
} catch(Error $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) [MyCustomError] /home/bjori/ex.php:6 You are doing it wrong! (112) [InvalidArgumentError]