在某些环境下,我们想从头掷出适才发生过的违例,出格是在用Exception捕捉所有大概的违例时。由于我们已拥有当前违例的句柄,所以只需简朴地从头掷出谁人句柄即可。下面是一个例子:
catch(Exception e) {
System.out.println("一个违例已经发生");
throw e;
}
从头“掷”出一个违例导致违例进入更高一级情况的违例节制器中。用于同一个try块的任何更进一步的catch从句仍然会被忽略。另外,与违例工具有关的所有对象城市获得保存,所以用于捕捉特定违例范例的更高一级的节制器可以从谁人工具里提取出所有信息。
若只是简朴地从头掷出当前违例,我们打印出来的、与printStackTrace()内的谁人违例有关的信息会与违例的发源地对应,而不是与从头掷出它的所在对应。若想安装新的仓库跟踪信息,可挪用fillInStackTrace(),它会返回一个非凡的违例工具。这个违例的建设进程如下:将当前仓库的信息填充到本来的违例工具里。下面列出它的形式:
//: Rethrowing.java
// 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 Throwable {
try {
f();
} catch(Exception e) {
System.out.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.out.println(
"Caught in main, e.printStackTrace()");
e.printStackTrace();
}
}
} ///:~
个中最重要的行号在注释内标志出来。留意第17行没有设为注释行。它的输出功效如下:
originating the exception in f()
Inside g(), e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.f(Rethrowing.java:8)
at Rethrowing.g(Rethrowing.java:12)
at Rethrowing.main(Rethrowing.java:24)
Caught in main, e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.f(Rethrowing.java:8)
at Rethrowing.g(Rethrowing.java:12)
at Rethrowing.main(Rethrowing.java:24)
因此,违例仓库路径无论如何城市记着它的真正起点,无论本身被反复“掷”了好屡次。
若将第17行标注(酿成注释行),而除掉对第18行的标注,就会换用fillInStackTrace(),功效如下:
originating the exception in f()
Inside g(), e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.f(Rethrowing.java:8)
at Rethrowing.g(Rethrowing.java:12)
at Rethrowing.main(Rethrowing.java:24)
Caught in main, e.printStackTrace()
java.lang.Exception: thrown from f()
at Rethrowing.g(Rethrowing.java:18)
at Rethrowing.main(Rethrowing.java:24)
由于利用的是fillInStackTrace(),第18行成为违例的新起点。
针对g()和main(),Throwable类必需在违例规格中呈现,因为fillInStackTrace()会生成一个Throwable工具的句柄。由于Throwable是Exception的一个基本类,所以有大概得到一个可以或许“掷”出的工具(具有Throwable属性),但却并非一个Exception(违例)。因此,在main()顶用于Exception的句柄大概丢失本身的方针。为担保所有对象均井井有条,编译器强制Throwable利用一个违例类型。举个例子来说,下述措施的违例便不会在main()中被捕捉到:
//: ThrowOut.java
public class ThrowOut {
public static void
main(String[] args) throws Throwable {
try {
throw new Throwable();
} catch(Exception e) {
System.out.println("Caught in main()");
}
}
} ///:~
也有大概从一个已经捕捉的违例从头“掷”出一个差异的违例。但如果这样做,会获得与利用fillInStackTrace()雷同的结果:与违例发源地有关的信息会全部丢失,我们留下的是与新的throw有关的信息。如下所示:
//: RethrowNew.java
// Rethrow a different object from the one that
// was caught
public class RethrowNew {
public static void f() throws Exception {
System.out.println(
"originating the exception in f()");
throw new Exception("thrown from f()");
}
public static void main(String[] args) {
try {
f();
} catch(Exception e) {
System.out.println(
"Caught in main, e.printStackTrace()");
e.printStackTrace();
throw new NullPointerException("from main");
}
}
} ///:~
#p#分页标题#e#
输出如下:
originating the exception in f()
Caught in main, e.printStackTrace()
java.lang.Exception: thrown from f()
at RethrowNew.f(RethrowNew.java:8)
at RethrowNew.main(RethrowNew.java:13)
java.lang.NullPointerException: from main
at RethrowNew.main(RethrowNew.java:18)
最后一个违例只知道本身来自main(),而非来自f()。留意Throwable在任何违例类型中都不是必须的。
永远不必体贴如何排除前一个违例,可能与之有关的其他任何违例。它们都属于用new建设的、以内存堆为基本的工具,所以垃圾收集器会自动将其排除。
