以下是对JAVA异常的继承机制的一些总结。
1. RuntimeException与Exception, Error不同点: 当方法体中抛出非RuntimeException(及其子类)时,方法名必须声明抛出的异常;但是当方法体中抛出RuntimeException(包括RuntimeException子类)时,方法名不必声明该可能被抛出的异常,即使声明了,JAVA程序在某个调用的地方,也不需要try-catch从句来处理异常。
- class TestA{
- //compiles fine.we don't need to claim the RuntimeException to be thrown here
- void method(){
- throw new RuntimeException();
- }
- }
- class TestB{
- void method() throws RuntimeException{
- throw new RuntimeException();
- }
- void invokeMethod(){
- //compiles fine. we don't need the try-catch clause here
- method();
- }
- }
- class TestC{
- //compiles error.we need to claim the Exception to be thrown on the method name
- void method(){
- throw new Exception();
- }
- }
- class TestD{
- //compiles fine.
- void method() throws Exception{
- throw new Exception();
- }
- }
以下所有的相关异常的特性都不包括RuntimeException及其子类。
2. 假如一个方法在父类中没有声明抛出异常,那么,子类覆盖该方法的时候,不能声明异常。
- class TestA{
- void method(){}
- }
- class TestB extends TestA{
- //complies error if the method overrided pertaining to the base class doesn't declare throwing exceptions
- void method() throws Exception{
- throw new Exception();
- }
- }
3. 假如一个方法在父类中声明了抛出异常,子类覆盖该方法的时候,要么不声明抛出异常,要么声明被抛出的异常继承自它所覆盖的父类中的方法抛出的异常。
- class TestA{
- void method() throws IOException{}
- }
- class TestB extends TestA{
- //compiles fine if current method does not throw any exceptions
- void method(){}
- }
- class TestC extends TestA{
- //compiles fine because InterruptedIOException is inherited from IOException which is thrown by the overrided method of the base class
- void method() throws InterruptedIOException{}
- }
- class TestD extends TestA{
- //compiles error because Exception thrown by current method is not inherited from IOException which is thrown by the overrided method of the base class
- void method() throws Exception{}
- }
4. 构造器不遵循上述规则,因为构造器不遵循JAVA的覆盖和重载规则。
- class TestA {
- public TestA() throws IOException {}
- public TestA(int i) {}
- }
- class TestC extends TestA {
- // compiles fine if current constructor doesn't throw anything.
- public TestC() { super(0); }
- }
- class TestB extends TestA {
- // compiles fine even if current constructor throws exceptions which don't
- // inherit from exceptions that are thrown by the overrided method of the
- // base class
- // this also means constructors don't conform the inheriting system of JAVA
- // class
- public TestB() throws Exception {}
- }
5. 当一个类继承某个类,以及实现若干个接口,而被继承的类与被实现的接口拥有共同的方法,并且该方法被覆盖时,它所声明抛出的异常必须与它父类以及接口一致。
- class ExceptionA extends Exception{
- }
- class ExceptionB extends Exception{
- }
- interface TestA{
- void method() throws ExceptionA;
- }
- abstract class TestB{
- abstract void method() throws ExceptionB;
- }
- class TestC extends TestB implements TestA{
- //compiles error
- public void method() throws ExceptionA{}
- }
- class TestD extends TestB implements TestA{
- //compiles error
- public void method() throws ExceptionB{}
- }
- class TestE extends TestB implements TestA{
- //compiles error
- public void method() throws ExceptionA,ExceptionB{}
- }
- class TestF extends TestB implements TestA{
- //compiles fine
- public void method(){}
- }