JAVA異常總結 ------ 繼承(轉載)

以下是對JAVA異常的繼承機制的一些總結。

 

1. RuntimeException與Exception, Error不同點: 當方法體中拋出非RuntimeException(及其子類)時,方法名必須聲明拋出的異常;但是當方法體中拋出RuntimeException(包括RuntimeException子類)時,方法名不必聲明該可能被拋出的異常,即使聲明瞭,JAVA程序在某個調用的地方,也不需要try-catch從句來處理異常。

  1. class TestA{  
  2.     //compiles fine.we don't need to claim the RuntimeException to be thrown here   
  3.     void method(){  
  4.         throw new RuntimeException();  
  5.     }  
  6. }  
  7. class TestB{  
  8.     void method() throws RuntimeException{  
  9.         throw new RuntimeException();  
  10.     }  
  11.       
  12.     void invokeMethod(){  
  13.         //compiles fine. we don't need the try-catch clause here   
  14.         method();  
  15.     }  
  16. }  
  17. class TestC{  
  18.       
  19.     //compiles error.we need to claim the Exception to be thrown on the method name    
  20.     void method(){  
  21.         throw new Exception();  
  22.     }  
  23. }  
  24. class TestD{  
  25.     //compiles fine.   
  26.     void method() throws Exception{  
  27.         throw new Exception();  
  28.     }  
  29. }  
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. 假如一個方法在父類中沒有聲明拋出異常,那麼,子類覆蓋該方法的時候,不能聲明異常。

  1. class TestA{  
  2.     void method(){}  
  3. }  
  4. class TestB extends TestA{  
  5.       
  6.     //complies error if the method overrided pertaining to the base class doesn't declare throwing exceptions   
  7.     void method() throws Exception{  
  8.         throw new Exception();  
  9.     }  
  10. }  
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. 假如一個方法在父類中聲明瞭拋出異常,子類覆蓋該方法的時候,要麼不聲明拋出異常,要麼聲明被拋出的異常繼承自它所覆蓋的父類中的方法拋出的異常。

  1. class TestA{     
  2.     void method() throws IOException{}     
  3. }     
  4. class TestB extends TestA{     
  5.     //compiles fine if current method does not throw any exceptions      
  6.     void method(){}     
  7. }     
  8. class TestC extends TestA{     
  9.     //compiles fine because InterruptedIOException is inherited from IOException which is thrown by the overrided method of the base class      
  10.     void method() throws InterruptedIOException{}     
  11. }     
  12. class TestD extends TestA{     
  13.     //compiles error because Exception thrown by current method is not inherited from IOException which is thrown by the overrided method of the base class      
  14.     void method() throws Exception{}     
  15. }    
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的覆蓋和重載規則。

  1. class TestA {  
  2.     public TestA() throws IOException {}  
  3.   
  4.     public TestA(int i) {}  
  5. }  
  6.   
  7. class TestC extends TestA {  
  8.     // compiles fine if current constructor doesn't throw anything.   
  9.     public TestC() { super(0); }  
  10. }  
  11.   
  12. class TestB extends TestA {  
  13.     // compiles fine even if current constructor throws exceptions which don't   
  14.     // inherit from exceptions that are thrown by the overrided method of the   
  15.     // base class   
  16.     // this also means constructors don't conform the inheriting system of JAVA   
  17.     // class   
  18.     public TestB() throws Exception {}  
  19. }  
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. 當一個類繼承某個類,以及實現若干個接口,而被繼承的類與被實現的接口擁有共同的方法,並且該方法被覆蓋時,它所聲明拋出的異常必須與它父類以及接口一致。

  1. class ExceptionA extends Exception{  
  2. }  
  3. class ExceptionB extends Exception{  
  4.       
  5. }  
  6. interface TestA{  
  7.     void method() throws ExceptionA;  
  8. }  
  9. abstract class TestB{  
  10.     abstract void method() throws ExceptionB;  
  11. }  
  12. class TestC extends TestB implements TestA{  
  13.     //compiles error   
  14.     public void method() throws ExceptionA{}  
  15. }  
  16. class TestD extends TestB implements TestA{  
  17.     //compiles error   
  18.     public void method() throws ExceptionB{}  
  19. }  
  20. class TestE extends TestB implements TestA{  
  21.     //compiles error   
  22.     public void method() throws ExceptionA,ExceptionB{}  
  23. }  
  24. class TestF extends TestB implements TestA{  
  25.     //compiles fine   
  26.     public void method(){}  
  27. }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章