获取局部变量

发布于 2024-10-11 05:26:37 字数 112 浏览 0 评论 0原文

当从已部署的应用程序获取堆栈跟踪作为错误报告时,获取实际变量值以重建抛出异常之前的系统状态也会很有帮助。

类似的事情在 Java 中可行吗?如何才能做到这一点?

干杯, 最大限度

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

等待我真够勒 2024-10-18 05:26:37

我很确定您无法获取堆栈跟踪中的局部变量,因为输出是从 StackTraceElement 实例构建的,该实例仅包含类、文件、方法和行号(请参阅 http://download.oracle.com/javase/6/docs/api/java/lang/ StackTraceElement.html)。

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

大姐,你呐 2024-10-18 05:26:37

看看这个工具:

http://www.chrononsystems.com/

它允许您跟踪堆栈跟踪,可以选择在应用程序的时间轴中返回过去。我自己还没有尝试过,但对于您所指的情况类型(意外异常)来说,它看起来确实很有希望。

很高兴听到它是否有帮助:)

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

空城旧梦 2024-10-18 05:26:37

如果您使用诸如Eclipse之类的IDE - 您可以使用调试工具在程序的整个执行过程中查看这一点。

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

岁吢 2024-10-18 05:26:37

发生这种情况的唯一方法是,是否可以通过异常捕获链中较高的可用变量或通过异常对象本身(在自定义异常的情况下)访问相关系统状态:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文