Java Exception Rethrowing
来源:互联网 发布:快消品网络商学院 编辑:程序博客网 时间:2024/05/20 04:48
Java Exception Rethrowing
------------- fillInStackTrace ()
The fillInStackTrace () method is to fill 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. It will return a reference to a Throwable object, which is useful when rethrow an exception or an exception.
For example 1,
public class FillStackTrace {
public static void f()throws Exception{
System.out.println("f() throws an Exception");
throw new Exception("From f()");
}
public static void g() throws Exception {
try{
f();
}catch(Exception e){
System.err.println("cault in g()");
e.printStackTrace();
throw e;
}
}
public static void main(String[] args) throws Exception{
try{
g();
}catch(Exception e){
System.err.println("cault in main");
e.printStackTrace();
}
}
}
Output:
f() throws an Exception
cault in g()
java.lang.Exception: From f()
at FillStackTrace.f(FillStackTrace.java:5)
at FillStackTrace.g(FillStackTrace.java:9)
at FillStackTrace.main(FillStackTrace.java:22)
cault in main
java.lang.Exception: From f()
at FillStackTrace.f(FillStackTrace.java:5)
at FillStackTrace.g(FillStackTrace.java:9)
at FillStackTrace.main(FillStackTrace.java:22)
You can see the out from the line “e.printStackTrace()” in main method:
at FillStackTrace.f(FillStackTrace.java:5)
at FillStackTrace.g(FillStackTrace.java:9)
at FillStackTrace.main(FillStackTrace.java:22)
The information you print about the exception in printStackTrace () of f() method includes the exception origin, not the place where you rethrow it. Because rethrowing an exception will cause it to go to the exception handlers in the next-higher context. Any further catch in the same try block will be ignored. Everything about the exception object is preserved, so the handler at the higher context that catches the specific exception type can extract all the information from that object.
If you want to change the origin of the exception to the place where you rethrow it, use fillInStackTrace (), which returns a Throwable object that it creates by stuffing the current stack information into the old exception object.
For example 2:
public class FillStackTrace {
public static void f() throws Exception{
System.out.println("f() throws an Exception");
throw new Exception("From f()");
}
public static void g() throws Throwable {
try{
f();
}catch(Exception e){
System.err.println("cault in g()");
e.printStackTrace();
throw e .fillInStackTrace();
}
}
public static void main(String[] args) throws Throwable{
try{
g();
}catch(Exception e){
System.err.println("cault in main");
e.printStackTrace();
}
}
}
Output:
f() throws an Exception
cault in g()
java.lang.Exception: From f()
at FillStackTrace.f(FillStackTrace.java:6)
at FillStackTrace.g(FillStackTrace.java:10)
at FillStackTrace.main(FillStackTrace.java:23)
cault in main
java.lang.Exception: From f()
at FillStackTrace.g(FillStackTrace.java:14)
at FillStackTrace.main(FillStackTrace.java:23)
The different between example 1 and example 2 are the rethrowing line and the exception specification of method main and g. Because fillInStackTrace () produces a reference to a Throwable object, since Throwable is the base class of Exception.
- Java Exception Rethrowing
- Java Exception: MISSING EXCEPTION
- java exception
- java-Exception
- Java exception
- Java exception
- Java Exception
- java Exception
- JAVA Exception
- Java Exception
- Java Exception
- java exception
- java EXception
- java Exception
- Java Exception
- Java Exception
- Java Exception
- Java Exception
- 判断点是否在一个三角形内部
- 在linux下一个有趣的STL文件IO问题
- 如何使用 Transact-SQL 执行事务处理
- SAP用户登录增强示例
- oracle 变量绑定 ORA-00903错误:无效表名
- Java Exception Rethrowing
- MSN消息接龙(2006.11 下)
- 三则小故事
- 文件和文件夹丢失权限, 命令行下修改文件访问控制权限,解决方案
- 职场黄金定律
- 黄褪技术——输入提示的效果
- pony12的2006年大事记(工作、生活、娱乐)
- windows 快速链接效果1
- windows 快速链接效果2