PHP尝试捕获异常
您好,我有这样的代码:
try
{
// Here I call my external function
do_some_work()
}
catch(Exception $e){}
问题是:如果 do_some_work() 有问题并产生错误,则此 try catch 会隐藏错误吗?
Hello I have a code like that :
try
{
// Here I call my external function
do_some_work()
}
catch(Exception $e){}
The question is: If the do_some_work() has a problem and produce an Error this try catch will hide the error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PHP 中有两种类型的错误。有例外,也有错误。
try..catch
将处理异常,但不会处理错误。为了捕获 PHP 错误,您需要使用
set_error_handler()
函数。简化事情的一种方法可能是让
set_error_handler()
在遇到错误时抛出异常。如果这样做,您需要小心行事,因为它有可能导致各种麻烦,但这将是一种让try..catch
处理所有 PHP 错误的方法。There are two types of error in PHP. There are exceptions, and there are errors.
try..catch
will handle exceptions, but it will not handle errors.In order to catch PHP errors, you need to use the
set_error_handler()
function.One way to simplify things mught be to get
set_error_handler()
to throw an exception when you encounter an error. You'd need to tread carefully if you do this, as it has the potential to cause all kinds of trouble, but it would be a way to gettry..catch
to work with all PHP's errors.如果
do_some_work()
抛出异常,它将被捕获并忽略。try/catch 结构对标准 PHP 错误没有影响,只对异常有影响。
If
do_some_work()
throws an exception, it will be catched and ignored.The try/catch construct has no effect on standard PHP errors, only on exceptions.
不,catch 无法捕获致命错误。你甚至不能使用错误处理程序。
如果您想捕获所有其他错误,请查看
ErrorException
和它专用于set_error_handler
:No, catch can not catch Fatal Errors. You can not even with an error handler.
If you want to catch all other errors, have a look for
ErrorException
and it's dedicated use withset_error_handler
: