特定例外与例外 [英] Specific Exceptions vs Exception

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

问题描述

这可能是一个非常愚蠢的问题,但是我试图理解为什么它很重要。为什么使用特定的异常而不是仅使用Exception如此重要。这是一个示例:

This may be a very dumb question, but I'm trying to understand why it is significant. Why is it important to use a specific exception instead of just using Exception. Here is an example:

List<String> testList = new ArrayList<String>();

try {
    testList.add(1, "String");
} catch (IndexOutOfBoundsException ex) {
    ex.printStackTrace();
}

上面的示例具有特定的例外情况,这会引起这种情况。为什么拥有 IndexOutOFBoundsException 而不是仅使用 Exception 如此重要。我知道将 Exception printStackTrace()一起使用将向您显示真正引发了什么异常。

The above example has the specific exception this would raise. Why is it important to have IndexOutOFBoundsException instead of just using Exception. I know using Exception with printStackTrace() will show you what exception is really being raised.

推荐答案

这是一个很好的问题,但这也是一个很大的问题。约书亚·布洛赫(Joshua Bloch)在他的着名著作《有效Java》中写下了整整一章的异常最佳实践。他与此有关的建议之一是:

This is a good question, but it's also very a big question. In his famous book, Effective Java, Joshua Bloch dedicates an entire chapter to Exception best practices. One of his recommendations that's relevant here is this:


对可恢复的条件使用检查异常,对编程错误使用运行时异常。

Use checked exceptions for recoverable conditions and runtime exceptions for programming errors.

与发布的代码相关的原因是,通过捕获Exception可以忽略可恢复条件和编程错误之间的区别。您是在说:我不在乎出什么问题,我会处理的。

The reason this is relevant to the code you posted is that by catching Exception you're ignoring the difference between "recoverable conditions" and "programming errors". You're saying, "I don't care what's wrong, I'm going to handle it."

虽然有一定的时间和地点可以进行这种处理,但通常是在应用程序的最高级别。例如,在未处理的异常的情况下,许多Web应用程序框架向用户显示漂亮的错误页面。他们使用类似于 catch Exception 的某种机制来执行此操作,以提供更好的用户体验并防止整个Web应用程序框架崩溃。

While there is a time and place for this sort of handling, it's usually at the very highest level of your application. For example, many web application frameworks display a nice-looking error page to the user in the case of an unhandled exception. They do this using some mechanism similar to catch Exception to provide a nicer user experience and prevent the entire web application framework from crashing.

另一方面,如果一些粗心的开发人员在低级代码中的某个地方 catch异常,然后将其捕获他不是故意的例外,它很有可能会以其他方式破坏应用程序,从而恶化用户体验并使调试更加困难。

On the other hand, if some careless developer were to catch Exception somewhere in low level code, and then caught an exception he didn't mean to, there is a good chance the application would break in other ways, worsening the user experience and making debugging far more difficult.


  • 有效Java ,第二版,作者Joshua Bloch


    • 第58项:将可检查的异常用于可恢复的条件,将运行时异常用于编程错误

    • 第61项:抛出适合于抽象的异常[这也意味着 catch 适合抽象的异常]

    • 项目65:不要忽略异常[捕获异常就像忽略整个类的可能的错误]

    • Effective Java, Second Edition, by Joshua Bloch
      • Item 58: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors
      • Item 61: Throw exceptions appropriate to the abstraction [which also implies to catch exceptions appropriate to the abstraction]
      • Item 65: Don't ignore exceptions [catching Exception is like ignoring a whole class of possible errors]

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

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