exceptions - Rethrowing an Exception

本文探讨了Java中异常处理的高级技巧,包括如何使用fillInStackTrace()方法更新异常的堆栈跟踪信息,以及如何抛出不同于捕获的异常对象。通过实例演示了这些操作的实际效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

To install new stack trace information, we can call fillInStackTrace(), which returns a Throwable object it creates by stuffing the current stack information into the old exception object.

case 1:

use fillInStackTrace() method

// exceptions/Rethrowing.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.
// Demonstrating fillInStackTrace()

public class Rethrowing {
  public static void f() throws Exception {
    System.out.println("originating the exception in f()");
    throw new Exception("thrown from f()");
  }

  public static void g() throws Exception {
    try {
      f();
    } catch (Exception e) {
      System.out.println("Inside g(), e.printStackTrace()");
      e.printStackTrace(System.out);
      throw e;
    }
  }

  public static void h() throws Exception {
    try {
      f();
    } catch (Exception e) {
      System.out.println("Inside h(), e.printStackTrace()");
      e.printStackTrace(System.out);
      throw (Exception) e.fillInStackTrace();
    }
  }

  public static void main(String[] args) {
    try {
      g();
    } catch (Exception e) {
      System.out.println("main: printStackTrace()");
      e.printStackTrace(System.out);
    }
    try {
      h();
    } catch (Exception e) {
      System.out.println("main: printStackTrace()");
      e.printStackTrace(System.out);
    }
  }
}
/* My Output:
originating the exception in f()
Inside g(), e.printStackTrace()
java.lang.Exception: thrown from f()
        at Rethrowing.f(Rethrowing.java:10)
        at Rethrowing.g(Rethrowing.java:15)
        at Rethrowing.main(Rethrowing.java:35)
main: printStackTrace()
java.lang.Exception: thrown from f()
        at Rethrowing.f(Rethrowing.java:10)
        at Rethrowing.g(Rethrowing.java:15)

        at Rethrowing.main(Rethrowing.java:35)
originating the exception in f()
Inside h(), e.printStackTrace()
java.lang.Exception: thrown from f()
        at Rethrowing.f(Rethrowing.java:10)
        at Rethrowing.h(Rethrowing.java:25)
        at Rethrowing.main(Rethrowing.java:41)
main: printStackTrace()
java.lang.Exception: thrown from f()
        at Rethrowing.h(Rethrowing.java:29)
        at Rethrowing.main(Rethrowing.java:41)
*/

fillInStackTrace

public Throwable fillInStackTrace()

Fills in the execution stack trace. This method records within this Throwable object information about the current state of the stack frames for the current thread.

If the stack trace of this Throwable is not writable, calling this method has no effect.

Returns:

a reference to this Throwable instance.

See Also:

printStackTrace()


case 2:

rethrow a different object from the one we caught.

// exceptions/RethrowNew.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.
// Rethrow a different object from the one you caught

class OneException extends Exception {
  OneException(String s) {
    super(s);
  }
}

class TwoException extends Exception {
  TwoException(String s) {
    super(s);
  }
}

public class RethrowNew {
  public static void f() throws OneException {
    System.out.println("originating the exception in f()");
    throw new OneException("thrown from f()");
  }

  public static void main(String[] args) {
    try {
      try {
        f();
      } catch (OneException e) {
        System.out.println("Caught in inner try, e.printStackTrace()");
        e.printStackTrace(System.out);
        throw new TwoException("from inner try");
      }
    } catch (TwoException e) {
      System.out.println("Caught in outer try, e.printStackTrace()");
      e.printStackTrace(System.out);
    }
  }
}
/* My Output:
originating the exception in f()
Caught in inner try, e.printStackTrace()
OneException: thrown from f()
        at RethrowNew.f(RethrowNew.java:22)
        at RethrowNew.main(RethrowNew.java:28)
Caught in outer try, e.printStackTrace()
TwoException: from inner try
        at RethrowNew.main(RethrowNew.java:32)
*/

Don’t worry about cleaning up the previous exception, or any exceptions. They’re all heap-based objects created with new, so the garbage collector automatically cleans them all up.

references:

1. On Java 8 - Bruce Eckel

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

3. https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html#fillInStackTrace--

4. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/exceptions/RethrowNew.java

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值