重新抛出异常

有时你希望把刚捕获的异常重新抛出,尤其是在使用 Exception 捕获所有异常的时候。
既然你已经得到了当前异常对象的引用,你可以直接把它重新抛出:


catch(Exception e) { 
  System.err.println("An exception was thrown");
throw e;
}  

重抛异常会把异常抛给上一级环境中的异常处理程序。同一个 try 块的后续 catch 子句
将被忽略。此外,异常对象的所有信息都得以保持,所以高一级环境中捕获此异常的处
理程序可以从这个异常对象中得到所有信息。
 
如果你只是把当前异常对象重新抛出,那么 printStackTrace( )方法显示的将是原来异
常抛出点的调用栈信息,而并非重新抛出点的信息。要想更新这个信息,你可以调用
fillInStackTrace( )方法,这将返回一个 Throwable 对象,它是通过把当前调用栈信
息填入原来那个异常对象而建立的。就像这样:
 
//: c09:Rethrowing.java
// Demonstrating fillInStackTrace()
import com.bruceeckel.simpletest.*; 


public class Rethrowing { 
private static Test monitor = new Test(); 
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 Throwable {
try { 
      f(); 
    } catch(Exception e) {
      System.err.println("Inside g(),e.printStackTrace()");
      e.printStackTrace(); 
throw e; // 17
// throw e.fillInStackTrace(); // 18
    }
  }
public static void
  main(String[] args) throws Throwable { 
try { 
      g(); 
    } catch(Exception e) {
      System.err.println( 
"Caught in main, e.printStackTrace()"); 
      e.printStackTrace(); 
    }
    monitor.expect(new String[] { 
"originating the exception in f()",
"Inside g(),e.printStackTrace()",
"java.lang.Exception: thrown from f()",
"%% \tat Rethrowing.f(.*?)",
"%% \tat Rethrowing.g(.*?)",
 






"%% \tat Rethrowing.main(.*?)",
"Caught in main, e.printStackTrace()",
"java.lang.Exception: thrown from f()",
"%% \tat Rethrowing.f(.*?)",
"%% \tat Rethrowing.g(.*?)",
"%% \tat Rethrowing.main(.*?)"
    });
  }
} ///:~


重要的几行用数字注释出来了。如果第 17 行没有注释掉(所示情况),那么无论异常
对象被重新抛出多少次,其调用栈信息始终是原始抛出地点的信息。
 
如果把第 17 行注释掉,第 18 行的注释解除,并使用 fillInStackTrace( ),那么运行
结果就会是:


originating the exception in f()
Inside g(),e.printStackTrace() 
java.lang.Exception: thrown from f() 
        at Rethrowing.f(Rethrowing.java:9) 
        at Rethrowing.g(Rethrowing.java:12)
        at Rethrowing.main(Rethrowing.java:23) 
Caught in main, e.printStackTrace() 
java.lang.Exception: thrown from f() 
        at Rethrowing.g(Rethrowing.java:18)
        at Rethrowing.main(Rethrowing.java:23) 


(此外还有 Test.expect( )输出的出错信息。)因为有了 fillInStackTrace( ),第 18
行就成了异常的新发生地了。
 
因为 fillInStackTrace( )返回的是对 Throwable 对象的引用,所以 g( )和 main( )的
异常说明中必须要有 Throwable 类的名称。既然 Throwable 是 Exception 的基类,
你就有可能抛出一个是 Throwable 而非 Exception 的对象,所以 main()中的捕获
Exception 的处理程序可能捕获不到这个对象。为了确保一切都能正常运行,编译器将
强制在异常说明里使用 Throwable。例如,下面程序中的异常将不能在 main( )里被捕
获:
 
//: c09:ThrowOut.java
// {ThrowsException}
public class ThrowOut { 
public static void
  main(String[] args) throws Throwable { 
try { 
throw new Throwable();

    } catch(Exception e) {
      System.err.println("Caught in main()"); 
    }
  }
} ///:~


你有可能会在捕获异常之后抛出另一种异常。这么做的话,将得到类似使用
fillInStackTrace( )的效果,有关原来异常发生地点的信息会丢失,剩下的是与新的抛
出地点有关的信息:
 
//: c09:RethrowNew.java
// Rethrow a different object from the one that was caught.
// {ThrowsException}
import com.bruceeckel.simpletest.*; 


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


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


public class RethrowNew { 
private static Test monitor = new Test(); 
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) throws TwoException {
try { 
      f(); 
    } catch(OneException e) {
      System.err.println( 
"Caught in main, e.printStackTrace()"); 
      e.printStackTrace(); 
throw new TwoException("from main()");
    }
    monitor.expect(new String[] { 
"originating the exception in f()",
"Caught in main, e.printStackTrace()",
"OneException: thrown from f()",
"\tat RethrowNew.f(RethrowNew.java:18)",

"\tat RethrowNew.main(RethrowNew.java:22)",
"Exception in thread \"main\" " +
"TwoException: from main()",
"\tat RethrowNew.main(RethrowNew.java:28)"
    });
  }
} ///:~


最后那个异常仅知道自己来自 main(),而对 f()一无所知。
 
你永远不用为清理前一个异常对象而担心,或者说为异常对象的清理担心。它们都是用

new在堆上创建的对象,所以垃圾回收器会自动把它们清理掉。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值