时间:2014-02-15所属栏目: PHP/PHP框架 来源: 未知 热度:
PHP代码:
<?php
class Foo
{
function __destruct() {
throw new Exception('abc');
}
}
$foo = new Foo();
?>
PHP5.3.6版本的执行结果是:
Fatal error: Exception thrown without a stack frame in Unknown on line 0
而PHP5.3.8的结果则是:
Fatal error: Uncaught exception ‘Exception’ with message ‘abc’ in C:\Users\hdj\Desktop\demo.php:5
Stack trace:
#0 [internal function]: Foo->__destruct()
#1 {main}
thrown in C:\Users\hdj\Desktop\demo.php on line 5
PHP手册里的解析是:
试图在析构函数中抛出一个异常会导致致命错误。
PHP5.3.8官方的更新日志:
Fixed bug #43175 (__destruct() throwing an exception with __call() causes segfault). (Dmitry)
所了解,有这么一种说法:PHP析构函数里抛出异常时,如果你注册了自己的异常处理函数,并且再次抛出异常就会出错。
PHP代码:
<?php
class MyException extends Exception
{
//
}
function throw_exception($msg='Unknown exception', $type='MyException', $code=0){
if(class_exists($type))
throw new $type($msg, $code);
else
die($msg);
}
class Foo
{
function __destruct() {
// throw new Exception('abc');
throw_exception('abc');
}
}
$foo = new Foo();
?>
PHP5.3.8环境下执行结果:
标签:exception, __destructFatal error: Uncaught exception ‘MyException’ with message ‘abc’ in C:\Users\hdj\Desktop\demo.php:9
Stack trace:
#0 C:\Users\hdj\Desktop\demo.php(18): throw_exception(‘abc’)
#1 [internal function]: Foo->__destruct()
#2 {main}
thrown in C:\Users\hdj\Desktop\demo.php on line 9