什么时候抛出异常?
异常是美妙的事情,但有时我担心我抛出太多异常。考虑这个例子:
类用户{
公共函数用户(用户){ // 查询数据库中的用户数据 if(!user) throw new ExistenceException('未找到用户'); }
}
我认为简单地返回 false(或在这种情况下将所有用户数据设置为 false)比抛出异常更有意义。
你更喜欢哪一个?
Exceptions are wonderful things, but I sometimes worry that I throw too many. Consider this example:
Class User {
public function User(user){ // Query database for user data if(!user) throw new ExistenceException('User not found'); }
}
I'd argue that it makes as much sense to simply return false (or set all user data to false in this case), rather than throwing an exception.
Which do you prefer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
异常情况抛出异常。
如果该情况是您在程序正常运行时所期望的情况,请检查它并报告错误。为不应该发生的事情保留异常,或者作为向其他程序员表明发生了不可恢复的事情的指示。
在您的情况下,返回
false
更有意义(或者可能使用 null 对象< /a> 作为返回值)。Throw exception in exceptional circumstances.
If the condition is something you expect in the normal run of the program, check for it and report an error. Reserve exceptions for things that should never happen, or as an indication to other programmers that something unrecoverable has occurred.
In your case, returning
false
makes more sense (or perhaps use a null object as a return value).我通常会使用异常作为最后的手段。如果这是永远不应该发生的事情并且语言支持它,我将使用某种断言(因为它的发生肯定是某种错误)。
如果可以避免,我更喜欢错误代码。
当然,在某些语言中,在某些情况下您别无选择。例如,C++ 构造函数可以没有返回值,并且拥有部分构造的对象可能很糟糕,因此抛出异常有时是最好的选择。
总而言之,如果您可以测试错误条件并以非基于异常的方式简单地将错误报告给调用者,我更愿意这样做。
I generally will use exceptions as a very last resort. If it is something that should never happen and the language support its, i'll use an assert of some sort (since it happening is surely a bug of some kind).
If it can be avoided, i'll prefer an error code.
There are of course some cases in some languages where you have little choice. For example, c++ constructors can have no return value, and it is probably bad to have a partially constructed object, so throwing an exception is sometimes the best option.
All in all, if you can test for the error condition and simply report the error to the caller in a non-exception based way, I'd prefer that.
这是我的经验法则:
当服务器或其他一些预期的硬件或资源不可用时,就会出现不寻常的情况。当发生异常时,您希望开发人员或一线支持人员收到警报,指出某些问题需要修复。
Here is my rule of thumb
Something out of the ordinary is when servers or some other expected hardware or resource is unavailable. When an exception occurs, you want a developer or first line support alerted that something needs fixing.
当发生非常糟糕或意外发生的情况时使用异常。通常,当出现不可恢复的错误时,您希望抛出异常,该错误可能会使代码状态处于不一致的状态。有时,当构造函数传递了无效参数 (IllegalArgumentException) 时,您还想引发异常以停止对象的创建。这些都是特例。
正如其他评论所说,谨慎使用异常并将其作为最后的手段。在 Java 中,抛出异常会暂停 JVM,因此您不希望将其用于正常的错误处理,因为这会大大降低应用程序的性能。
Use exceptions when something really bad or unexpected happens. Generally, you want to throw an exception when you have an unrecoverable error that can leave the state of your code in an inconsistent state. Sometimes you also want to throw an exception to halt the creation of an object when the constructor has been passed a non-valid argument (IllegalArgumentException). These are all exceptional cases.
As other comments have said, use exceptions sparingly and as a last resort. In Java, throwing an exception pauses the JVM, so you don't want to use for normal error handling as it will greatly decrease the performance of your application.