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. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值