Java异常的限制
来源:互联网 发布:爱淘宝 1元红包 最新 编辑:程序博客网 时间:2024/06/16 16:39
class BaseballException extends Exception{}class Foul extends BaseballException{}class Strike extends BaseballException{}abstract class Inning{public Inning() throws BaseballException{}public void event() throws BaseballException{}public abstract void atBat() throws Strike, Foul;public void walk(){}}class StormException extends Exception{}class RainedOut extends StormException{}class PopFoul extends Foul{}interface Storm{public void event() throws RainedOut;public void rainHard() throws RainedOut;}public class StormyInning extends Inning implements Storm{public StormyInning() throws RainedOut, BaseballException{}public StormyInning(String s) throws Foul, BaseballException{}//! public void event() throws RainedOut{}//Interface CANNOT add exceptions to exsiting methods from the base calsspublic void rainHard() throws RainedOut{}//if the method does not already exist in the base class, the exception is OKpublic void event(){}//you can choose not to throw exceptions, even if the base version doespublic void atBat() throws PopFoul{}//overridden methods can throw inherited exceptionpublic static void main(String[] args){try{StormyInning si = new StormyInning();si.atBat();}catch(PopFoul e){System.out.println("Pop foul");}catch(RainedOut e){System.out.println("Rained out");}catch(BaseballException e){System.out.println("Generic baseball exception");}try{Inning i = new StormyInning();//upcast, what happensi.atBat();//you must catch the exceptions from the base-class version of the method:}catch(Strike e){System.out.println("Strike");}catch(Foul e){System.out.println("Foul");}catch(RainedOut e){System.out.println("Rained out");}catch(BaseballException e){System.out.println("Generic baseball exception");}}}
1. 如果StormyInning类在扩展Inning类的同时又实现了Storm的接口,那么Storm里的event()方法就不能改变在Inning中的event()方法的异常接口。
2. 派生类构造器不能捕获基类构造器抛出的异常。
3. 异常说明本身不属于方法类型的一部分,方法类型是由方法的名字和参数的类型组成,因此,不能基于异常说明来重载方法。
阅读全文
0 0
- Java异常的限制
- Java中异常的限制
- java方法重写时的异常限制
- Java编程思想12.9-异常的限制
- Java 异常限制
- 从头认识java-10.7 使用异常的限制汇总
- 《Thinking in Java》——异常的限制,子类异常与父类异常接口的关系
- Java Applet的限制
- java Exception的限制
- Java继承的限制
- 用户输入内容长度限制的异常
- java编程思想9.5——异常的限制(继承与实现接口情况下,读书笔记)
- [转]战胜Java的限制!
- java 虚拟机的内存限制
- Java带有限制的通配符
- Java 抽象类的限制
- java抽象类的限制
- java 继承 继承的限制
- Python中的Scapy初探之二
- java数据结构之实现 二叉树 以及二叉树的遍历方式(三)
- Squid代理服务器(三)——ACL访问控制
- 修改eclipse 中用的jdk版本
- Excel使用数据透视图配合切片器制作动态数据图表
- Java异常的限制
- 题目17:单调递增最长子序列
- 【Python】Python3 List insert()方法
- webservice的简单开发步骤
- 部署两个项目时报 Web app root system property already set to different value:
- 自定义单例 AlertDialog
- 72.单词转变
- cocos2dx 3.1.x lua mvc代码修改成官方代码示例
- 利用PHPExcel导出数据到excel