以下是對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();
- }
- }
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();
- }
- }
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{}
- }
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 {}
- }
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(){}
- }