本文介绍了为什么只能在方法签名中声明某些异常被抛出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当声明一个带有IllegalAccessException的方法时,eclipse强制我使用
将该方法声明为抛出异常

when declaring a methods with "IllegalAccessException" eclipse forces me todeclare the method as throwing an exception

public void a()  throws IllegalAccessException {
 if(x == 1){
   throw new IllegalAccessException("TEST);
 }
}

并且在方法b中使用IllegalStateException我不需要将方法声明为抛出异常

and in method b that uses "IllegalStateException" i dont need to declare the method as throw an exception

public void b()  {
 if(x == 1){
   throw new IllegalStateException("TEST);
 }
}

thous exception之间有什么不同

强迫我声明抛出异常的方法
而另一个不是

what is the different between thous exception
that one forces me tho declare the method that throw an exceptionand the other is not

谢谢

推荐答案

因为 IllegalAccessException 不是 RuntimeException (即是检查异常)和 IllegalStateException RuntimeException (即未经检查的异常)。

Because IllegalAccessException is not RuntimeException (i.e. is checked exception) and IllegalStateException is a RuntimeException (i.e. is unchecked exception).

阅读本文以获取更多信息:

Read this for more information: difference between java.lang.RuntimeException and java.lang.Exception

这个关于Oracle(duh!)网站的解释:

And this explanation on Oracle (duh!) site: http://download.oracle.com/javase/tutorial/essential/exceptions/catchOrDeclare.html

第二种异常是错误。这些是应用程序外部的特殊情况,应用程序通常无法预测或恢复。例如,假设应用程序成功打开文件以进行输入,但由于硬件或系统故障而无法读取文件。不成功的读取将抛出java.io.IOError。应用程序可能会选择捕获此异常,以便通知用户该问题 - 但它也可能有助于程序打印堆栈跟踪并退出。

The second kind of exception is the error. These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from. For example, suppose that an application successfully opens a file for input, but is unable to read the file because of a hardware or system malfunction. The unsuccessful read will throw java.io.IOError. An application might choose to catch this exception, in order to notify the user of the problem — but it also might make sense for the program to print a stack trace and exit.

错误不受Catch或Specify Requirement 的约束。错误是由Error及其子类指示的异常。

Errors are not subject to the Catch or Specify Requirement. Errors are those exceptions indicated by Error and its subclasses.

第三种异常是运行时异常。这些是应用程序内部的特殊条件,应用程序通常无法预测或恢复。这些通常表示编程错误,例如逻辑错误或API的不当使用。例如,考虑前面描述的应用程序将文件名传递给FileReader的构造函数。如果逻辑错误导致将null传递给构造函数,则构造函数将抛出NullPointerException。应用程序可以捕获此异常,但消除导致异常发生的错误可能更有意义。

The third kind of exception is the runtime exception. These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from. These usually indicate programming bugs, such as logic errors or improper use of an API. For example, consider the application described previously that passes a file name to the constructor for FileReader. If a logic error causes a null to be passed to the constructor, the constructor will throw NullPointerException. The application can catch this exception, but it probably makes more sense to eliminate the bug that caused the exception to occur.

运行时异常不受Catch影响或指定要求。运行时异常是RuntimeException及其子类指示的异常。

Runtime exceptions are not subject to the Catch or Specify Requirement. Runtime exceptions are those indicated by RuntimeException and its subclasses.

这篇关于为什么只能在方法签名中声明某些异常被抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 22:30
查看更多