JAVA异常总结 ------ 继承

来源:互联网 发布:中小企业网络推广优化 编辑:程序博客网 时间:2024/06/16 17:29

以下是对JAVA异常的继承机制的一些总结。

1. RuntimeException与Exception, Error不同点: 当方法体中抛出非RuntimeException(及其子类)时,方法名必须声明抛出的异常;但是当方法体中抛出RuntimeException(包括RuntimeException子类)时,方法名不必声明该可能被抛出的异常,即使声明了,JAVA程序在某个调用的地方,也不需要try-catch从句来处理异常。

 

[java] view plaincopyprint?
  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. }  

 

以下所有的相关异常的特性都不包括RuntimeException及其子类。

2. 假如一个方法在父类中没有声明抛出异常,那么,子类覆盖该方法的时候,不能声明异常。


[java] view plaincopyprint?
  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. }  


 

3. 假如一个方法在父类中声明了抛出异常,子类覆盖该方法的时候,要么不声明抛出异常,要么声明被抛出的异常继承自它所覆盖的父类中的方法抛出的异常。

[java] view plaincopyprint?
  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. }    


 

4. 构造器不遵循上述规则,因为构造器不遵循JAVA的覆盖和重载规则。


[java] view plaincopyprint?
  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. }  


5. 当一个类继承某个类,以及实现若干个接口,而被继承的类与被实现的接口拥有共同的方法,并且该方法被覆盖时,它所声明抛出的异常必须与它父类以及接口一致。

[java] view plaincopyprint?
  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
原创粉丝点击