码迷,mamicode.com
首页 > 其他好文 > 详细

Throwable中的fillInStackTrace

时间:2014-07-08 17:47:01      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:exception   throwable   fillinstacktrace   

fillInStackTrace 方法的作用就是一追可以追溯到栈的底部。

转载一个测试类:

<span style="font-family:Microsoft YaHei;font-size:14px;">package com.exception;

/**
 * Created with IntelliJ IDEA.
 * User: 菜鸟大明
 * Date: 14-7-7
 * Time: 下午10:42
 * To change this template use File | Settings | File Templates.
 */
class Exception2 {
    Throwable th =  new Throwable();;
    public Exception2() {
        System.out.println("in constructor");
    }
    public void a() {
        c();
    }
    public void b() {
        System.out.println("in b");
        th.fillInStackTrace();
        th.printStackTrace(System.out);
        System.out.println("in b");
    }
    public void c(){
        b();
        th.fillInStackTrace();
        System.out.println("in c");
        th.printStackTrace(System.out);
        System.out.println("in c");
    }
    public static void main(String [] args) {
        Exception2 t3 = new Exception2();
        t3.a();
    }
}
</span>
上述代码的执行顺序为 main()→a()→b()→c()

打印结果为:

in constructor
in b
java.lang.Throwable
    at com.exception.Exception2.b(Exception2.java:23)
    at com.exception.Exception2.c(Exception2.java:29)
    at com.exception.Exception2.a(Exception2.java:18)
    at com.exception.Exception2.main(Exception2.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

in b
in c
java.lang.Throwable
    at com.exception.Exception2.c(Exception2.java:30)
    at com.exception.Exception2.a(Exception2.java:18)
    at com.exception.Exception2.main(Exception2.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

in c

如果把fillInStackTrace 注释掉的话,输出结果则为:

in constructor
in b
java.lang.Throwable
    at com.exception.Exception2.<init>(Exception2.java:11)
    at com.exception.Exception2.main(Exception2.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

in b
in c
java.lang.Throwable
    at com.exception.Exception2.<init>(Exception2.java:11)
    at com.exception.Exception2.main(Exception2.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

in c

Throwable中的fillInStackTrace,布布扣,bubuko.com

Throwable中的fillInStackTrace

标签:exception   throwable   fillinstacktrace   

原文地址:http://blog.csdn.net/zhao9tian/article/details/37542529

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!