您的位置:首页 > 编程语言 > Java开发

Java每一天4 Thinking P268

2016-07-03 16:10 585 查看
package com.pm;

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
/*originating the exception in f()
Inside g(), e.printStackTrace()
Caught in main, e.printStackTrace()
java.lang.Exception: thrown from f()
at com.pm.Rethrowing.f(Rethrowing.java:6)
at com.pm.Rethrowing.g(Rethrowing.java:11)
at com.pm.Rethrowing.main(Rethrowing.java:35)
java.lang.Exception: thrown from f()
at com.pm.Rethrowing.f(Rethrowing.java:6)
at com.pm.Rethrowing.g(Rethrowing.java:11)
at com.pm.Rethrowing.main(Rethrowing.java:35)*/

//throw e.fillInStackTrace(); // 18
/*
* originating the exception in f()
Inside g(), e.printStackTrace()
Caught in main, e.printStackTrace()
java.lang.Exception: thrown from f()
at com.pm.Rethrowing.f(Rethrowing.java:6)
at com.pm.Rethrowing.g(Rethrowing.java:11)
at com.pm.Rethrowing.main(Rethrowing.java:23)
java.lang.Exception: thrown from f()
at com.pm.Rethrowing.g(Rethrowing.java:17)
at com.pm.Rethrowing.main(Rethrowing.java:23)
*/
}
}

public static void main(String[] args) throws Throwable {
try {
g();
} catch (Exception e) {
System.out.println("Caught in main, e.printStackTrace()");
e.printStackTrace();
}
}
}


fillInStackTrace() 重新记录抛出信息
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java