首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Exception::getPrevious

(PHP 5 >= 5.3.0, PHP 7)

Exception::getPrevious — Returns previous Exception

Description

代码语言:javascript
复制
final public Throwable Exception::getPrevious ( void )

Returns previous exception (the third parameter of Exception::__construct()).

Parameters

This function has no parameters.

Return Values

Returns the previous Throwable if available or NULL otherwise.

Changelog

Version

Description

7.0.0

The return type declaration changed to Throwable.

Examples

Example #1 Exception::getPrevious() example

Looping over, and printing out, exception trace.

代码语言:javascript
复制
<?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());
}
?>

The above example will output something similar to:

代码语言:javascript
复制
/home/bjori/ex.php:8 Something happened (911) [MyCustomException]
/home/bjori/ex.php:6 You are doing it wrong! (112) [InvalidArgumentException]

See Also

  • Throwable::getPrevious() - Returns the previous Throwable

← Exception::getMessage

Exception::getCode →

代码语言:txt
复制
 © 1997–2017 The PHP Documentation Group

Licensed under the Creative Commons Attribution License v3.0 or later.

扫码关注腾讯云开发者

领取腾讯云代金券