Getting local variables

一曲冷凌霜 提交于 2019-12-10 08:21:00

问题


When getting a stacktrace as error report from an application that is already deployed, it would be helpful to also get the actual variable values to reconstruct the system's state at the point before the exception was thrown.

Is anything like that feasible in Java and how could one do that?

Cheers, Max


回答1:


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).




回答2:


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 :)




回答3:


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




回答4:


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);
}


来源:https://stackoverflow.com/questions/4620430/getting-local-variables

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