Java异常的限制

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 calss
	public void rainHard() throws RainedOut{}//if the method does not already exist in the base class, the exception is OK
	public void event(){}//you can choose not to throw exceptions, even if the base version does
	public void atBat() throws PopFoul{}//overridden methods can throw inherited exception
	
	public 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 happens
			i.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值