异常继承

java异常继承结构图

这里写图片描述

可以从图中发现, Error, Exception都是继承于Throwable,
关于Error,Exception的异同点可以查看这里写链接内容


本文描述在继承中有关异常的总结:

  1. 基类中方法没有声明抛出异常时, 其子类重写该方法时也不能抛出任何异常
package com.mjlf.myBatis.accessControl.animal;

/**
 * Created by a123 on 16/12/25.
 */



public class ExceptionA extends ExceptionB {
    @Override
    public void testException() throws Exception{

    }

    public static void main(String[] args){}
}

class ExceptionB{
    public void testException(){

    }
}

/*
Error:(11, 17) java: com.mjlf.myBatis.accessControl.animal.ExceptionA中的testException()无法覆盖com.mjlf.myBatis.accessControl.animal.ExceptionB中的testException()
  被覆盖的方法未抛出java.lang.Exception
*/

2.基类方法抛出异常,子类要么不抛出异常, 要么抛出异常为基类方法抛出异常或基类方法抛出异常的子异常

package com.mjlf.myBatis.accessControl.animal;

/**
 * Created by a123 on 16/12/25.
 */


public class ExceptionA extends ExceptionB {
    @Override
    public void testException() throws Exception{

    }

    public static void main(String[] args){}
}

class ExceptionB{
    public void testException() throws ExceptionC{

    }
}

class ExceptionC extends Exception{

}

/**
Error:(11, 17) java: com.mjlf.myBatis.accessControl.animal.ExceptionA中的testException()无法覆盖com.mjlf.myBatis.accessControl.animal.ExceptionB中的testException()
  被覆盖的方法未抛出java.lang.Exception
*/
package com.mjlf.myBatis.accessControl.animal;

/**
 * Created by a123 on 16/12/25.
 */


public class ExceptionA extends ExceptionB {
    @Override
    public void testException() throws ExceptionC{

    }

    public static void main(String[] args){
        System.out.println("yes");
    }
}

class ExceptionB{
    public void testException() throws Exception{

    }
}

class ExceptionC extends Exception{

}

/**
yes

Process finished with exit code 0

*/

3 . 构造器不遵循上述规则,因为构造器不遵循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 {}  
}  

4 . 当一个类同时继承另一个类和实现一个接口是, 而这个接口和类中同时用一个相同的方法,但是这个方法抛出了不同的异常时, 这时子类方法要么选择不抛出异常, 要么抛出异常为接口方法和基类方法抛出异常的交集, 否则该方法编译报错

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() {
    }
}  
/**
Error:(23, 17) java: com.mjlf.myBatis.accessControl.animal.TestC中的method()无法覆盖com.mjlf.myBatis.accessControl.animal.TestB中的method()
  被覆盖的方法未抛出com.mjlf.myBatis.accessControl.animal.ExceptionA
Error:(29, 17) java: com.mjlf.myBatis.accessControl.animal.TestD中的method()无法实现com.mjlf.myBatis.accessControl.animal.TestA中的method()
  被覆盖的方法未抛出com.mjlf.myBatis.accessControl.animal.ExceptionB
  Error:(35, 17) java: com.mjlf.myBatis.accessControl.animal.TestE中的method()无法实现com.mjlf.myBatis.accessControl.animal.TestA中的method()
  被覆盖的方法未抛出com.mjlf.myBatis.accessControl.animal.ExceptionB
*/
package com.mjlf.myBatis.accessControl.animal;

//交集使用
class ExceptionA extends Exception {
    public static void main(String[] args) {

    }
}

class ExceptionB extends Exception {

}

interface TestA {
    void method() throws ExceptionA, ExceptionB;
}

abstract class TestB {
    abstract void method() throws ExceptionB;
}

class TestC extends TestB implements TestA {
    public void method() throws ExceptionB {
    }
}

class TestD extends TestB implements TestA {
    public void method() throws ExceptionB {
    }
}

class TestE extends TestB implements TestA {
    public void method() throws ExceptionB {
    }
}

class TestF extends TestB implements TestA {
    //compiles fine  
    public void method() {
    }
}  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值