PHP尝试捕获异常 [英] PHP try catch exceptions

查看:90
本文介绍了PHP尝试捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有这样的代码:

Hello I have a code like that :

try
{
    // Here I call my external function
    do_some_work()
}
catch(Exception $e){}

问题是:如果do_some_work()有问题并产生错误,则此尝试捕获将隐藏错误?

The question is: If the do_some_work() has a problem and produce an Error this try catch will hide the error?

推荐答案

PHP中有两种错误。有异常,有错误。

There are two types of error in PHP. There are exceptions, and there are errors.

try..catch 将处理异常,但不会处理错误。

try..catch will handle exceptions, but it will not handle errors.

为了捕获PHP错误,您需要使用 set_error_handler()函数

In order to catch PHP errors, you need to use the set_error_handler() function.

一种简化事物的方法当遇到错误时,使 set_error_handler()引发异常。如果这样做,则需要谨慎行事,因为它可能会引起各种麻烦,但这是将 try..catch 获取的一种方法。使用所有PHP的错误。

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 get try..catch to work with all PHP's errors.

这篇关于PHP尝试捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆