什么时候应该使用PHP异常? [英] When should you use PHP Exceptions?

查看:57
本文介绍了什么时候应该使用PHP异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多演示简单尝试的教程,例如打开文件的过程.但是我从来没有见过一个大型的,真实的"例子.有人可以为我提供一些他们会使用例外情况的案例吗?真的有必要扩展异常类以引发异常吗?最后,当引发异常时,是否会导致脚本退出()??还是将其记录下来并继续执行脚本?

I've seen lots of tutorials that demo simple try catches with, say, the act of opening a file. But I've never seen a big, "real" example. Can someone provide me with a few cases in which they have or would use exceptions? And is it really necessary to extend the exception class just to throw an exception? And finally, when throwing exceptions, does it cause the script to exit(); ? Or, does it log it and continue on with script execution?

推荐答案

我们在项目中广泛使用异常.

We use exceptions extensively within our projects.

一个特定的实例用于要求用户登录或注册的操作.我们将异常用于错误条件下的流控制.如果当前用户未登录,则抛出异常.然后,异常处理程序将它们重定向到登录页面.

One specific instance is for actions that require the user to be logged in or upon registration. We use Exceptions for flow control on error conditions. If the current user is not logged in we throw an exception. The exception handler then redirects them to the loggin page.

以我们的注册操作为例,我们像下面这样扩展Exception:

Using our registration action as an example, we extend the Exception like this:

class RegistrationFailed extends Exception {}

现在在注册代码中的 catch 语句中,我们可以测试RegistrationFailed异常并进行相应的处理.否则,当异常不是RegistrationFailed时,由于我们对此不感兴趣,我们允许它冒泡.

Now in our catch statement within the registration code we can test for the RegistrationFailed exception and handle it accordingly. Otherwise, when the exception is not a RegistrationFailed, we allow it to bubble up because we are not interested in it.

try {
    // do registration here
}
catch(RegistrationFailed $e) {
    // handle the failed registration
}
catch(SomeOtherException $e) {
    // handle other errors like this...
}

// All other errors will not be caught and will bubble up

另一个示例在我们的包装器类中,开发人员必须对其进行扩展.我们使用Reflection来确保子类正确实现了它们的方法并提供了正确的接口.如果不是,我们会通过异常通知该类的开发人员,让他们知道子类必须提供特定的接口或方法.

Another example is within our wrapper classes which developers must extended. We use Reflection to ensure the child classes have properly implemented their methods and provided the correct interface. If not we notify the developer of that class via Exceptions letting them know a specific interface or method must be provided by the child class.

我已经可以听到有关您不应该对流控制使用错误处理!"的评论.但是,对于上面讨论的项目,这是必要的.

I can already hear the comments about "You shouldn't use error handling for flow control!" however, for the project discussed above, it was necessary.

在程序的正常流程中,由于许多验证规则可能会失败,因此可能会导致注册失败,例如密码太短.

In the normal flow of the program a failed registration is expected due to the many validation rules that might fail, like a password that's too short.

但是,这是一个ajax应用程序,因此有人可能会在未登录时尝试手动访问ajax网址.此作为异常,因此我们将其作为此类处理.

However, it's an ajax application, so it's possible that someone might try to access an ajax url manually, when they are not logged in. This is as exception and thus we handle it as such.

这篇关于什么时候应该使用PHP异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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