2014-02-27 71 views
1

在PHP中,我使用set_exception_handler函数构建了错误处理程序。它执行我的自定义异常处理程序。我希望PHP在执行我的处理程序后也执行默认的异常处理程序。这是我的代码。执行自定义错误句柄后执行默认异常处理程序

function handleException(exception $e){ 
    echo $e->getMessage(); 
    restore_exception_handler(); 
} 

set_exception_handler('handleException'); 

echo $e->getMessage()执行,但那么即使使用restore_exception_handler后默认的PHP异常处理程序没有得到执行。那么,我怎样才能使它工作?

+2

http://chat.stackoverflow.com/transcript/message/14976548#14976548 – Danack

+0

你对'default exception handler'有什么意思? – hek2mgl

回答

2

你应该恢复它

function handleException(exception $e){ 
    echo $e->getMessage(); 
    restore_exception_handler(); 
    throw $e; //This triggers the previous exception handler 
} 

set_exception_handler('handleException'); 
+0

哦!我没有想到在处理程序里面使用'throw',结果浪费了很多时间 –

1

后触发前一个异常处理程序manual对此也很清楚:

设置默认的异常处理程序,如果一个例外是不是一个try/catch块内抓。执行将在调用exception_handler后停止。

之后没有机会运行任何代码。

但是但是,你可以把它明确:

try { 
    // some code that throws an Exception 
} catch(Exception $e) { 
    handleException($e); 
    // .. run custom handler now 
} 

没有必要在这里使用restore_exception_handler()

相关问题