在Exception上转储变量的状态 [英] Dumping state of variables on Exception

查看:175
本文介绍了在Exception上转储变量的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以在出现异常时转储所有局部变量的状态,以便更好地了解导致异常的环境状态。变量 idsToDump 下面在运行时是未知的,我想找出集合中的值导致NPE的状态。

I was wondering if there is a way to dump the state of all local variables when there is an exception, to get a better idea of the state of the environment that caused the exception. Below the variable idsToDump is unknown at run time and I want to find out the state at which value in the collection is causing the NPE.

示例:

public static void main(String[] args) {
    HashMap<Integer, String> employees = new HashMap<Integer, String>();
    employees.put(1, "James");

    Integer[] idsToDump = new Integer[] { 1, 2, 3 };
    for (Integer employeeId : idsToDump) {
        String name = employees.get(employeeId).toLowerCase();
        System.out.println(name + " is employee number: " + employeeId);
    }

}

输出:

james is employee number: 1
Exception in thread "main" java.lang.NullPointerException

问题:
我是否可以传递一些JVM参数来转储有关局部变量当前状态的信息?即我们得到

Question: Is there some JVM argument that I can pass to dump information about the current state of the local variables? ie we get

java.lang.NullPointerException

和(这是我之后的部分)

and (this is the part I'm after)

values: employeeId=2

我希望能够在客户端站点上执行此操作,因此无法访问Eclipse或调试工具,只寻找JVM参数,也不能进行代码更改。我看过他们但却找不到任何东西。与此同时,我也会一直在那里搜索;)

I want to be able to do this on a client site, so no access to Eclipse or debugging tools, looking for just JVM arguments, can't make code changes either. I have looked through them but couldn't really find anything. In the meantime I'll keep searching there too ;)

推荐答案

考虑到你的所有限制,我不能推荐别的东西来自jdb。解雇那个坏孩子并开始逐行逐步完成客户端代码。我知道你说 没有 调试工具,但除非它们只是一个JRE环境,否则你应该已经安装了jdb。

Given all your restrictions, I can't recommend anything else apart from jdb. Fire that bad boy up and start stepping through the client code line by line. I know you said no debugging tools, but unless they are a JRE only environment you should have jdb installed already.

这篇关于在Exception上转储变量的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆