集册 Java实例教程 抛出异常以隐藏原始异常的位置

抛出异常以隐藏原始异常的位置

欢马劈雪     最近更新时间:2020-01-02 10:19:05

427
抛出异常以隐藏原始异常的位置
// 来自 时代Java - nowjava.com

public class Main {

  public static void main(String[] args) {

    try {

      m1();

    } catch (MyException e) {

      // Print the stack trace

      e.printStackTrace();

    }

  }


  public static void m1() throws MyException {

    try {

      m2();
      /*
      NowJava.com - 时  代  Java
      */

    } catch (MyException e) {

      e.fillInStackTrace();

      throw e;

    }

  }


  public static void m2() throws MyException {

    throw new MyException("An error has occurred.");

  }

}


class MyException extends Exception {

  public MyException() {

    super();

  }


  public MyException(String message) {

    super(message);

  }


  
展开阅读全文