Getting local variables

一个人想着一个人 提交于 2019-12-05 12:53:20

I'm pretty sure you cannot get the local variables in the stacktrace as the output is built from instance of StackTraceElement which only contains, the class, the file, the method and the line number (see http://download.oracle.com/javase/6/docs/api/java/lang/StackTraceElement.html).

Have a look at this tool:

http://www.chrononsystems.com/

It allows you to keep track of the stack trace with the option to go back in time in the application's timeline. I haven't tried it myself but it looks really promising for the type of situation you are referring to (unexpected Exceptions).

It will be good to hear if it helped :)

If you use an IDE such as Eclipse - you can use debugging tools to view this through the entire execution of the program.

The only way that's going to happen is if the system state in question is reachable through either variables available higher up in the exception-catching chain, or via the exception object itself (in the case of a custom exception):

public class MySpiffyException extends RuntimeException
{
    final private int foo;
    final private String bar;

    public MySpiffyException(String message, int foo, String bar) { 
       super(message); this.foo = foo; this.bar = bar; 
    }
    public MySpiffyException(Throwable cause, int foo, String bar) { 
       super(cause); this.foo = foo; this.bar = bar; 
    }
    public int getFoo() { return this.foo; }
    public String getBar() { return this.bar; }
}

...

public void someCode() {
   ...

   int foo = ...;
   String bar = ...;

   if (foo > 0)
      throw new MySpiffyException(foo, bar);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!