• java 异常的限制


    一.

    1.) 在覆盖方法的时候,只能抛出在基类方法的异常说明里列出的那些异常

    2.) 在基类构造器声明的异常,在子类必须抛出,子类的构造器可以抛出任何异常,但是必须抛出基类构造器的异常

    3.) 在基类和接口方法声明的异常,子类覆盖的方法可以不抛出基类和接口方法声明的异常以外的异常,但可以少或不抛出

    4.) 不能基于异常重载方法

    5.) 子类没有向上转型为基类和接口时,可以不捕获基类和接口的异常,反之.如有向上转型,必须捕获基类和接口的异常

    package exceptions;
    //: exceptions/StormyInning.java
    // Overridden methods may 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 {
      public 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 {
      public void event() throws RainedOut;
      public 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 Foul, 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:
      public void rainHard() throws RainedOut{}
      // You can choose to not throw any exceptions,
      // even if the base version does:
      public void event() {}
      // Overridden methods can throw inherited exceptions:
      public void atBat() throws PopFoul{} 
         //public void atBat() throws PopFoul,RainedOut {}不能throws出基类方法没有的异常
      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");
        }
      }
    } ///:~
  • 相关阅读:
    20135223何伟钦—信息安全系统设计基础第十三周学习总结
    20135223何伟钦—信息安全系统设计基础第十二周学习总结
    20135223何伟钦—信息安全系统设计基础第十一周学习总结
    20135223何伟钦—家庭作业汇总
    20135223何伟钦—信息安全系统设计基础第十周学习总结
    20135223何伟钦—第十章家庭作业
    20135223何伟钦—第六章家庭作业
    20135223何伟钦—信息安全系统设计基础第九周学习总结
    20135223/20135234/20135229小组——亚博 Arduino智能小车实践报告
    20135223何伟钦—信息安全系统设计基础期中总结
  • 原文地址:https://www.cnblogs.com/jiangfeilong/p/10301718.html
Copyright © 2020-2023  润新知