Getting local variables

When receiving stacktrace as an error report from an application that has already been deployed, it would also be useful to get the actual values โ€‹โ€‹of the variables to restore the state of the system at the point before the exception was thrown.

In Java, something like this is possible and how can this be done?

Cheers, Max

+4
source share
4 answers

I am sure that you cannot get local variables on the stack, since the output is built from an instance of StackTraceElement that contains only the class, file, method and line number (see http://download.oracle.com/javase/6/docs/ api / java / lang / StackTraceElement.html ).

+2
source

Take a look at this tool:

http://www.chrononsystems.com/

It allows you to track the stack trace with the ability to return in time on the application timeline. I have not tried it myself, but it looks very promising for the type of situation you are talking about (unexpected exceptions).

It would be nice to know if this helped :)

+2
source

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

+1
source

The only way that this will happen is the state of the system in question is available through any variables available above in the catch-catch chain or through 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); } 
0
source

Source: https://habr.com/ru/post/1334582/


All Articles