English 中文(简体)
为什么只能是 例外情形被宣布为在方法签名中投下的
原标题:Why must only certain Exceptions be declared as thrown in method signature

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

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

以及在使用“法律承认”的方法b中,有意需要宣布该方法为一种例外。

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

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

感谢

最佳回答

因为IllegalAccessException不是RuntimeException(即为例外),而IllegalStateException(即为不受限制的例外)。

阅读更多信息: java.lang.RuntimeException and java.lang.Exception

http://download.oracle.com

The first kind of exception is the checked exception. These are exceptional conditions that a well-written application should anticipate and recover from. For example, suppose an application prompts a user for an input file name, then opens the file by passing the name to the constructor for java.io.FileReader. Normally, the user provides the name of an existing, readable file, so the construction of the FileReader object succeeds, and the execution of the application proceeds normally. But sometimes the user supplies the name of a nonexistent file, and the constructor throws java.io.FileNotFoundException. A well-written program will catch this exception and notify the user of the mistake, possibly prompting for a corrected file name. Checked exceptions are subject to the Catch or Specify Requirement. All exceptions are checked exceptions, except for those indicated by Error, RuntimeException, and their subclasses.

第二类例外是错误。 这些是申请之外的特殊条件,申请通常不能预期或收回。 例如,假设申请成功开启了输入档案,但由于硬件或系统故障而无法阅读档案。 失败的读本将 throw。 申请可以选择追索这一例外情况,以便向使用者通报问题,但也可能使方案具有印制笔迹和出走的意义。

www.un.org/Depts/DGACM/index_spanish.htm 误差不受要求或具体要求限制。 错误是Error及其子类指出的例外情况。

第三类例外是临时例外。 这些是申请内部的特殊条件,申请通常不能预期或收回。 通常表明方案拟定方面的弊端,如逻辑错误或不当使用预报工具。 例如,审议此前描述的申请,将档案名称提交给建筑商,以便存档。 如果逻辑错误导致无法向建筑商出售,则建筑商将“Null” 。 申请可以满足这一例外,但消除造成例外的弊端可能更有意义。

www.un.org/Depts/DGACM/index_spanish.htm 停职例外情况不受要求限制。 停职例外是RuntimeException及其子类所示。

问题回答

非法占领是一种检查的例外。 必须宣布或处理经核实的例外情况,因为它们是预期的行动结果。

非法国家极端主义是一个不受控制的例外。 一般认为方案拟定错误是无限制的例外,预计不会作为正常方案执行的一部分收到。 如果方案拟定有错误,这些例外将按缺省方式印成标准错误,以细微追踪,从而使开发商能够确定方案拟定的错误是什么。

如您在JDK的Javadoc中看到的那样,IllegalStateExceptionRuntimeException,但与不同。 非法使用

正如Javadoc所说:

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.

A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.

这解释了差异。

Java的部分设计是,有些例外需要放弃声明,有些则没有。 那些没有被称作“RuntimException”的人有时间例外。 非法占领不是偶然的例外情况,非法国家极端主义是存在的。

差异背后的逻辑是,一般例外是针对任何时间可能出现的例外情况,不论方案执行人做什么,如果方案执行者想要重新制定其法典,他们总是应当由方案管理人处理。 非法获取或非法获取可能是由于硬件或出入许可的故障造成的,而这种许可不属于方案者的控制范围。 软件在出现时需要做一些事情,而且用扔 throw条款迫使方案者思考要做什么。 非法国家极端主义是某种方案错误的一种症状,因此,不一定需要处理。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...