Mer*_*kos 8 php exception-handling exception
你好我有这样的代码:
try
{
// Here I call my external function
do_some_work()
}
catch(Exception $e){}
Run Code Online (Sandbox Code Playgroud)
问题是:如果do_some_work()出现问题并产生错误,那么try catch会隐藏错误吗?
Spu*_*ley 12
PHP中有两种类型的错误.有例外,也有错误.
try..catch 将处理异常,但它不会处理错误.
为了捕获PHP错误,您需要使用该set_error_handler()函数.
简化事情的一种方法是set_error_handler()在遇到错误时抛出异常.如果你这样做,你需要谨慎行事,因为它有可能造成各种麻烦,但这将是一种解决try..catch所有PHP错误的方法.
产生致命错误
不,捕获不能捕获致命错误.你甚至不能使用错误处理程序.
如果您想捕获所有其他错误,请查看ErrorException并专用于set_error_handler:
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");
/* Trigger exception */
strpos();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19269 次 |
| 最近记录: |