注意:第23行代码和output的第45-48行
- public class TestException {
- public static void main(String[] args) {
- TestException te = new TestException();
- te.f1();
- System.out.println("main");
- }
- void f1(){
- try{
- f2();
- }catch(Exception e){
- e.printStackTrace();
- }
- System.out.println("f1");
- }
- void f2() throws Exception{
- try{
- f3();
- }catch(Exception e){
- e.printStackTrace();
- e.fillInStackTrace();
- throw e;
- }
- System.out.println("f2");
- }
- void f3()throws Exception{
- throw new Exception();
- }
- }
- output:
- f1
main
java.lang.Exception
at TestException.f3(TestException.java:30)
at TestException.f2(TestException.java:20)
at TestException.f1(TestException.java:11)
at TestException.main(TestException.java:5)
java.lang.Exception
at TestException.f2(TestException.java:23)
at TestException.f1(TestException.java:11)
at TestException.main(TestException.java:5)
结论:
fillInStackTrace每次执行的时候,会清空原来的栈内的trace信息。然后在当前的调用位置处重新建立trace信息。