为什么我们不必为RuntimeException添加try-catch? [英] Why we don't have to add try-catch to a RuntimeException?

查看:139
本文介绍了为什么我们不必为RuntimeException添加try-catch?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下,为什么我们不必在 RuntimeException 中添加try-catch块,而我们应该使用其他异常呢?

I want to ask why we don't have to add try-catch block to a RuntimeException while we should do that with other exceptions?

我的意思是:

public class Main {
    public static void main(String[] args) {
       throw new RuntimeException();
    }
}

编辑:
当我说: code>抛出新的RuntimeException(); 它是如此清楚,有一个异常会发生,那么为什么编译器不禁止?

Edit : when I say : throw new RuntimeException(); it is so clear that there is an exception will happen ,so why the compiler doesn't forbid that ?

推荐答案

这是因为它是一个未选中的异常。它不需要被明确声明或捕获。另请参阅有关此主题的Sun教程

That's because it's an unchecked exception. It doesn't need to be explicitly declared or catched. Also see the Sun tutorial on the subject.

更新:一般来说,您只应该抛出一个 RuntimeException (最好是< javadoc中列出的href =http://java.sun.com/javase/6/docs/api/java/lang/RuntimeException.html =noreferrer>子类)来表示主叫做错了即传递一个 null 参数(然后抛出 NullPointerException )或非法参数(然后抛出 IllegalArgumentException ),或者方法在错误的时刻被调用/ state(然后抛出 IllegalStateException )等等。调用者应该修复他们的代码以避免这种情况。例如。如果参数不为null,或者参数是否为正确的格式/语法,或者确保在正确的时刻调用该方法,请事先检查。

Update: in general, you should only throw a RuntimeException (preferably one of its subclasses listed in the javadoc) to signal that the caller is doing it wrong. I.e. passing a null argument (then throw NullPointerException), or an illegal argument (then throw IllegalArgumentException), or the method is called at the wrong moment/state (then throw IllegalStateException), etcetera. The caller is supposed to fix their code to avoid that. E.g. checking beforehand if the argument is not null, or if the argument is in correct format/syntax, or ensuring that the method is called at the right moment.

如果有一个特定的情况,应该抛出一个运行时异常,你不能使用一个特定的子类,那么你应该扩展它,并在新的异常的javadoc和调用方法中正确地记录它,例如 ConfigurationException扩展了RuntimeException ,因为调用代码在使用前没有正确配置应用程序/ API。这应该足以证明最终用户(另一位开发人员)相应地采取行动。

If there is a specific situation which should throw a runtime exception and you can't use one of its specific subclasses, then you are supposed to extend it and document it properly in the new exception's javadoc and in the calling method, e.g. ConfigurationException extends RuntimeException for the case that the calling code hasn't configured the application/API properly before use. This should signal the enduser (the other developer) sufficiently to take action accordingly.

简而言之: RuntimeExceptions 应该以编程方式识别由故障引起的可恢复的问题代码流或配置(阅读:开发人员的故障)。已检查 例外情况 应该以编程方式识别出由于外部控制代码之外的意外状况(例如数据库关闭,文件I / O错误,错误的最终用户输入等)导致的可恢复的问题。 错误 应该以编程方式识别不可恢复的问题(例如,内存不足,初始化程序中的异常等)。

In nutshell: RuntimeExceptions should identify programmatically recoverable problems which are caused by faults in code flow or configuration (read: developer's faults). Checked Exceptions should identify programmatically recoverable problems which are caused by unexpected conditions outside control of code (e.g. database down, file I/O error, wrong enduser input, etc). Errors should identify programmatically unrecoverable problems (e.g. out of memory, exception inside an initializer, etc).

这篇关于为什么我们不必为RuntimeException添加try-catch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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