trigger_error与抛出异常 [英] trigger_error vs. throwing exceptions

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

问题描述

类似的问题被问到这里,但是由于答案没有回答我的问题,我在问:

A similar question was asked here, but as the answers didn't answer my question, I'm asking:

我几乎从未使用过 trigger_error ,总是抛出异常,因为在我看来,错误是遗留的。但我改变了主意,我认为他们可以并存。有触发错误的情况更有意义。

I've almost never used trigger_error, always thrown exceptions instead, since in my mind errors are legacy. But I've changed my mind, I think they can co-exist. There are cases when triggering errors make more sense.

我正在更新这个图书馆,这个问题涉及发送方法,但是是一般的。这是我的推理:

I'm updating this library, this question concerns the send method, but is general enough. This is my reasoning:


  • 如果未设置API密钥常量,那不是可以收到的错误。这是一个编程错误,应该这样对待。

  • If an API key constant is not set, that is not a catchable error. That is a programming error, and should be treated as such.

如果电子邮件地址无效,那应该是可以收获的。这很可能是用户错误。

If an email address is invalid, that should be catchable. This is most likely a user error.

我是loco吗?这是不必要和烦人的,还是有意义的?

Am I loco? Is this unnecessary and annoying, or does it make sense?

推荐答案

如果我使用图书馆,我真的很讨厌使用try-catch块和旧样式错误检查。即使缺少的API密钥使库不可用,它仍然是应用程序的一部分。

If I'd use the library, I would really hate to use both try-catch block and old style error checking. And even if the missing API key renders the library unusable, it's still part of application.

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

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