Change Exception type and rethrow : throw « Statement Control « Java Tutorial






class MyException extends Exception {
  MyException() {
    super("My Exception");
  }
}

class YourException extends Exception {
  YourException() {
    super("Your Exception");
  }
}

class ChainDemo {
  public static void main(String[] args) {
    try {
      someMethod1();
    } catch (MyException e) {
      e.printStackTrace();
    }
  }

  static void someMethod1() throws MyException {
    try {
      someMethod2();
    } catch (YourException e) {
      System.out.println(e.getMessage());
      MyException e2 = new MyException();
      e2.initCause(e);
      throw e2;
    }
  }

  static void someMethod2() throws YourException {
    throw new YourException();
  }
}








4.11.throw
4.11.1.Demonstrate throw.
4.11.2.Change Exception type and rethrow