JAVA异常总结 ------ 继承
来源:互联网 发布:知乎电脑版下载 编辑:程序博客网 时间:2024/05/24 06:38
以下是对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 herevoid 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 heremethod();}}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 exceptionsvoid 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// classpublic 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 errorpublic void method() throws ExceptionA{}}class TestD extends TestB implements TestA{//compiles errorpublic void method() throws ExceptionB{}}class TestE extends TestB implements TestA{//compiles errorpublic void method() throws ExceptionA,ExceptionB{}}class TestF extends TestB implements TestA{//compiles finepublic void method(){}}
- JAVA异常总结 - 继承
- JAVA异常总结 ------ 继承
- JAVA异常总结 ------ 继承
- JAVA异常总结 ------ 继承(转载)
- JAVA异常总结 ------ 继承(转载)
- Java继承中的异常
- java继承覆盖总结
- java 继承的总结
- java继承总结
- Java知识总结-继承
- Java继承小总结
- java 继承总结
- java继承覆盖总结
- java继承知识总结
- java 继承总结
- java继承覆盖总结
- java annotation继承总结
- java继承知识总结
- MFC两种重载控件消息响应的方法
- 什么是反向代理,如何利用反向代理提高网站性能
- ASP.NET MVC生命周期
- ZendFrameWork application.ini配置
- WIN蓝屏文件查看工具windbg
- JAVA异常总结 ------ 继承
- 看我们的WIFI-Robot无线远程遥控智能机器人
- javascript 打印时去掉页眉页脚
- PHP特效应用 实用珍藏代码举例
- oracle中游标的使用,示例
- Arnold置乱
- 接口的无敌解释!
- 我勒个去,居然还有350万美国人拨号上网?
- coolshell 中发现的网络学习资源