exceptions - Restrictions

When we override a method, we can throw only the exceptions specified in the base-class version of the method. This is a useful restriction, since it means code that works with the base class will automatically work with any object derived it (a fundamental OOP concept), including exceptions.

// exceptions/StormyInning.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Overridden methods can throw only the exceptions
// specified in their base-class versions, or exceptions
// derived from the base-class exceptions

class BaseballException extends Exception {}

class Foul extends BaseballException {}

class Strike extends BaseballException {}

abstract class Inning {
  Inning() throws BaseballException {}

  public void event() throws BaseballException {
    // Doesn't actually have to throw anything
  }

  public abstract void atBat() throws Strike, Foul;

  public void walk() {} // Throws no checked exceptions
}

class StormException extends Exception {}

class RainedOut extends StormException {}

class PopFoul extends Foul {}

interface Storm {
  void event() throws RainedOut;

  void rainHard() throws RainedOut;
}

public class StormyInning extends Inning implements Storm {
  // OK to add new exceptions for constructors, but you
  // must deal with the base constructor exceptions:
  public StormyInning() throws RainedOut, BaseballException {}

  public StormyInning(String s) throws BaseballException {}
  // Regular methods must conform to base class:
  // - void walk() throws PopFoul {} //Compile error
  // Interface CANNOT add exceptions to existing
  // methods from the base class:
  // - public void event() throws RainedOut {}
  // If the method doesn't already exist in the
  // base class, the exception is OK:
  @Override
  public void rainHard() throws RainedOut {}
  // You can choose to not throw any exceptions,
  // even if the base version does:
  @Override
  public void event() {}
  // Overridden methods can throw inherited exceptions:
  @Override
  public void atBat() throws PopFoul {}

  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");
    }
    // Strike not thrown in derived version.
    try {
      // What happens if you upcast?
      Inning i = new StormyInning();
      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");
    }
  }
}

Please note the comments in the above code.

In Inning, you see that both the constructor and the event() method say they will throw an exception, but they never do. This is legal because it forces the user to catch any exceptions that might be added in overridden versions of event()

When StormyInning extends Inning and implements Storm, the event() method in Storm cannot change the exception interface of event() in Inning. Again, this makes sense because otherwise you’d never know if you were catching the correct thing when working with the base class.

The restriction on exceptions does not apply to constructors.

A derived-class constructor cannot catch exceptions thrown by its base-class constructor.

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/exceptions/StormyInning.java

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值