异常继承
来源:互联网 发布:淘宝特卖鱼具 编辑:程序博客网 时间:2024/06/06 04:25
java异常继承结构图
可以从图中发现, Error, Exception都是继承于Throwable,
关于Error,Exception的异同点可以查看这里写链接内容
本文描述在继承中有关异常的总结:
- 基类中方法没有声明抛出异常时, 其子类重写该方法时也不能抛出任何异常
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{}/**yesProcess 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.ExceptionAError:(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 0
- 异常继承
- JAVA异常总结 - 继承
- JAVA异常总结 ------ 继承
- 异常继承应用
- Python 异常继承体系
- JAVA异常总结 ------ 继承
- Java继承中的异常
- 10.4 异常与继承
- C++异常与继承
- 继承、 异常 、多态
- C++异常与继承
- 异常的继承范围
- 自定义异常--继承RuntimeException
- 【第十一课】异常---异常与继承
- C++继承类的异常
- 异常类的继承体系
- 关于hibernate继承关系异常
- Python 异常类继承树
- Spring+Websocket+Redis
- 第23天 网络编程、udp、tcp,ip
- MAC电脑,sublimeText2安装package control和 pylinker 、 Jedi 插件
- C#多线程同步(一)【计数器】
- mybatis 报错 Specified class is an interface
- 异常继承
- 【逻辑思考】我们要有把坏事看成好事的能力
- nginx搭建支持http和rtmp协议的流媒体服务器之二 【转】
- Thinking in java4 Exception
- 【OpenCV入门教程之八】线性邻域滤波专场:方框滤波、均值滤波与高斯滤波
- c++网络多人五子棋
- 在树莓派上使用Python实现对温度,湿度以及光照强度的实施测量
- 第十二章 迭进
- SDK4/5/6/7,相册、拍照及裁剪功能及遇见的坑