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. 异常说明本身不属于方法类型的一部分,方法类型是由方法的名字和参数的类型组成,因此,不能基于异常说明来重载方法。