JAVA异常总结 ------ 继承
来源:互联网 发布:中小企业网络推广优化 编辑:程序博客网 时间:2024/06/16 17:29
以下是对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();
- }
- }
以下所有的相关异常的特性都不包括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();
- }
- }
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{}
- }
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 {}
- }
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(){}
- }
0 0
- JAVA异常总结 - 继承
- JAVA异常总结 ------ 继承
- JAVA异常总结 ------ 继承
- JAVA异常总结 ------ 继承(转载)
- JAVA异常总结 ------ 继承(转载)
- Java继承中的异常
- java继承覆盖总结
- java 继承的总结
- java继承总结
- Java知识总结-继承
- Java继承小总结
- java 继承总结
- java继承覆盖总结
- java继承知识总结
- java 继承总结
- java继承覆盖总结
- java annotation继承总结
- java继承知识总结
- Theano编程细节(一)
- Visual C++ Tips: 忽略 LNK4099 警告
- 读书笔记--理解RemoteViews
- Makefile之常用函数介绍
- Swift学习笔记 -- 流程控制
- JAVA异常总结 ------ 继承
- Unity学习笔记(1) --- 一切皆组件
- 深入探索spring技术内幕(四): 剖析@Resource注解实现原理与注解注入
- NSArray (多种数组元素排序)
- Linux su 与 su -
- union 和union all
- 【Codeforces Round 276 (Div 2)A】【暴力】Factory 数增余数直到余数为0
- 如何在LINUX内核中添加模块
- org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleT