62,628
社区成员
发帖
与我相关
我的任务
分享
public class ThrowableDemo {
public static void function1() throws Exception {
throw new Exception("this is thrown from function1()");
}
public static void function2() throws Exception {
try {
function1();
} catch (Exception e) {
System.err.println("Inside function2():");
throw new Exception(e);
}
}
public static void main(String[] args) {
try {
function2();
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出:
Inside function2():
java.lang.Exception: java.lang.Exception: this is thrown from function1()
at fundamental.fundamental.oop.ThrowableDemo.function2(ThrowableDemo.java:16)
at fundamental.fundamental.oop.ThrowableDemo.main(ThrowableDemo.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.Exception: this is thrown from function1()
at fundamental.fundamental.oop.ThrowableDemo.function1(ThrowableDemo.java:8)
at fundamental.fundamental.oop.ThrowableDemo.function2(ThrowableDemo.java:13)
... 6 more
我的理解这里应该是1more才对吧,因为Caused by 中的异常链与上面的异常链只有main处是重复的,那这里为什么会是6?
这6个信息具体是什么?
[/quote]
function1仅仅比function2多一层的栈信息,那就是‘at fundamental.fundamental.oop.ThrowableDemo.function1(ThrowableDemo.java:8)’,其他都是一样的栈路径,6 指的就是上面的那个异常栈中除了第一条外的其他六个。 public class ThrowableDemo {
public static void function1() throws Exception {
throw new Exception("this is thrown from function1()");
}
public static void function2() throws Exception {
try {
function1();
} catch (Exception e) {
System.err.println("Inside function2():");
throw new Exception(e);
}
}
public static void main(String[] args) {
try {
function2();
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出:
Inside function2():
java.lang.Exception: java.lang.Exception: this is thrown from function1()
at fundamental.fundamental.oop.ThrowableDemo.function2(ThrowableDemo.java:16)
at fundamental.fundamental.oop.ThrowableDemo.main(ThrowableDemo.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.Exception: this is thrown from function1()
at fundamental.fundamental.oop.ThrowableDemo.function1(ThrowableDemo.java:8)
at fundamental.fundamental.oop.ThrowableDemo.function2(ThrowableDemo.java:13)
... 6 more
我的理解这里应该是1more才对吧,因为Caused by 中的异常链与上面的异常链只有main处是重复的,那这里为什么会是6?
这6个信息具体是什么?






public class ThrowableDemo {
public static void function1() throws Exception {
throw new Exception("this is thrown from function1()");
}
public static void function2() throws Exception {
try {
function1();
} catch (Exception e) {
System.err.println("Inside function2():");
throw new Exception(e);
}
}
public static void main(String[] args) {
try {
function2();
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出:
Inside function2():
java.lang.Exception: java.lang.Exception: this is thrown from function1()
at fundamental.fundamental.oop.ThrowableDemo.function2(ThrowableDemo.java:16)
at fundamental.fundamental.oop.ThrowableDemo.main(ThrowableDemo.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.Exception: this is thrown from function1()
at fundamental.fundamental.oop.ThrowableDemo.function1(ThrowableDemo.java:8)
at fundamental.fundamental.oop.ThrowableDemo.function2(ThrowableDemo.java:13)
... 6 more
我的理解这里应该是1more才对吧,因为Caused by 中的异常链与上面的异常链只有main处是重复的,那这里为什么会是6?
这6个信息具体是什么?
[/quote]
function1仅仅比function2多一层的栈信息,那就是‘at fundamental.fundamental.oop.ThrowableDemo.function1(ThrowableDemo.java:8)’,其他都是一样的栈路径,6 指的就是上面的那个异常栈中除了第一条外的其他六个。 [/quote]
明白了,谢谢,这样写可能会看得更清楚一些;
public class TestPrintStackTrace {
public static void f() throws Exception {
throw new Exception("出问题啦!");
}
public static void g() throws Exception {
try {
f();
} catch (Exception e) {
System.out.println("---------------------------------------------");
for(StackTraceElement es:e.getStackTrace()){
System.out.println(es);
}
System.out.println("---------------------------------------------");
throw new Exception(e);
}
}
public static void main(String[] args) {
try {
g();
} catch (Exception e) {
System.out.println("+++++++++++++++++++++++++++++++++++");
for(StackTraceElement es:e.getStackTrace()){
System.out.println(es);
}
System.out.println("+++++++++++++++++++++++++++++++++++");
}
}
}
得到:
Main方法中的栈信息:
+++++++++++++++++++++++++++++++++++
fundamental.fundamental.oop.TestPrintStackTrace.g(TestPrintStackTrace.java:23)
fundamental.fundamental.oop.TestPrintStackTrace.main(TestPrintStackTrace.java:31)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:497)
com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
+++++++++++++++++++++++++++++++++++
g方法中的栈信息:
---------------------------------------------
fundamental.fundamental.oop.TestPrintStackTrace.f(TestPrintStackTrace.java:10)
fundamental.fundamental.oop.TestPrintStackTrace.g(TestPrintStackTrace.java:15)
fundamental.fundamental.oop.TestPrintStackTrace.main(TestPrintStackTrace.java:31)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:497)
com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
---------------------------------------------
这样就可以看到上面忽略掉的信息了,也就是g方法中的
fundamental.fundamental.oop.TestPrintStackTrace.main(TestPrintStackTrace.java:31)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:497)
com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)