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

 

 

以下所有的相关异常的特性都不包括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. }  
 

 

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

 

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

 

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

其他还有很多异常,我就不一一列举了,我要说明的是,一个合格的程序员,需要对程序中常见的问题有相当的了解和相应的解决办法,否则仅仅停留在写程序而不会改程序的话,会极大影响到自己的开发的。关于异常的全部说明,在api里都可以查阅。 算术异常类:ArithmeticExecption 空指针异常类:NullPointerException 类型强制转换异常ClassCastException 数组负下标异常:NegativeArrayException 数组下标越界异常:ArrayIndexOutOfBoundsException 违背安全原则异常:SecturityException 文件已结束异常:EOFException 文件未找到异常:FileNotFoundException 字符串转换为数字异常:NumberFormatException 操作数据库异常:SQLException 输入输出异常:IOException 方法未找到异常:NoSuchMethodException java.lang.AbstractMethodError 抽象方法错误。当应用试图调用抽象方法时抛出。 java.lang.AssertionError 断言错。用来指示一个断言失败的情况。 java.lang.ClassCircularityError 类循环依赖错误。在初始化一个类时,若检测到类之间循环依赖则抛出该异常java.lang.ClassFormatError 类格式错误。当Java虚拟机试图从一个文件中读取Java类,而检测到该文件的内容不符合类的有效格式时抛出。 java.lang.Error 错误。是所有错误的基类,用于标识严重的程序运行问题。这些问题通常描述一些不应被应用程序捕获的反常情况。 java.lang.ExceptionInInitializerError 初始化程序错误。当执行一个类的静态初始化程序的过程中,发生了异常时抛出。静态初始化程序是指直接包含于类中的static语句段。 java.lang.IllegalAccessError 违法访问错误。当一个应用试图访问、修改某个类的域(Field)或者调用其方法,但是又违反域或方法的可见性声明,则抛出该异常java.lang.IncompatibleClassChangeError 不兼容的类变化错误。当正在执行的方法所依赖的类定义发生了不兼容的改变时,抛出该异常。一般在修改了应用中的某些类的声明定义而没有对整个应用重新编译而直接运行的情况下,容易引发该错误。 java.lang.InstantiationError 实例化错误。当一个应用试图通过Java的new操作符构造一个抽象类或者接口时抛出该异常. java.lang.InternalError 内部错误。用于指示Java虚拟机发生了内部错误。 java.lang.LinkageError 链接错误。该错误及其所有子类指示某个类依赖于另外一些类,在该类编译之后,被依赖的类改变了其类定义而没有重新编译所有的类,进而引发错误的情况。 java.lang.NoClassDefFoundError 未找到类定义错误。当Java虚拟机或者类装载器试图实例化某个类,而找不到该类的定义时抛出该错误。 java.lang.NoSuchFieldError 域不存在错误。当应用试图访问或者修改某类的某个域,而该类的定义中没有该域的定义时抛出该错误。 java.lang.NoSuchMethodError 方法不存在错误。当应用试图调用某类的某个方法,而该类的定义中没有该方法的定义时抛出该错误。 java.lang.OutOfMemoryError 内存不足错误。当可用内存不足以让Java虚拟机分配给一个对象时抛出该错误。 java.lang.StackOverflowError 堆栈溢出错误。当一个应用递归调用的层次太深而导致堆栈溢出时抛出该错误。 java.lang.ThreadDeath 线程结束。当调用Thread类的stop方法时抛出该错误,用于指示线程结束。 java.lang.UnknownError 未知错误。用于指示Java虚拟机发生了未知严重错误的情况。 java.lang.UnsatisfiedLinkError 未满足的链接错误。当Java虚拟机未找到某个类的声明为native方法的本机语言定义时抛出。 java.lang.UnsupportedClassVersionError 不支持的类版本错误。当Java虚拟机试图从读取某个类文件,但是发现该文件的主、次版本号不被当前Java
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值