Java Exception 在超类和子类间的关系

两点需要注意:

1 子类的构造函数可以扔出比超类更广泛的异常,但必须包含超类的构造函数扔出的异常

2 子类的方法扔出的异常一定要比超类的“更窄”,这样向上转型时才不会出错

下面是一个例子:

//球类异常基类
class BaseballException extends Exception {}
//犯规
class Foul extends BaseballException {}
//打架
class Strike extends BaseballException {}

//一局球
abstract class Inning {
	public Inning() throws BaseballException {}
	public void event() throws BaseballException {
		//Doesn't actually have to throw anything
	}
	public void atBat() throws Strike, Foul {
		System.out.println("atBat() in superclass Inning");
	}
	public void walk() {} //Throws no checked exceptions
}

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 walk() {}
	
	public void rainHard() throws RainedOut {}
	
	//下面这样写编译器会报错
	//public void event() throws RainedOut {}
	//这样写也是错的
	//public void event() throws BaseballException {}
	//子类可以不扔出异常(比超类更窄),这样向上转型时就不会出错
	public void event() {}
	//重载的方法可以扔出比超类少的异常或扔出超类异常的子类
	public void atBat() throws PopFoul {
		System.out.println("atBat() in subclass StormyInning");
	}
	
	
	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();
			i.atBat();
		} 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");
		}
	}
}
/*
此程序的输出为:
atBat() in subclass StormyInning
atBat() in subclass StormyInning

即使子类向上转型为超类,然后调用超类的某个方法,其内部也会调用这个子类的方法,这是多态的体现
*/ 




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值