Java – dump the state of a variable in case of an exception

I want to know if there is a way to dump the state of all local variables when an exception occurs, so as to better understand the environment state causing the exception The following variable idstodump is unknown at runtime. I want to find out which value in the collection causes the state of NPE

Example:

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

}

Output:

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

Question: are there any JVM parameters that I can pass dump information about the current state of local variables? That is, we get

java.lang.NullPointerException

And (this is the part after me)

values: employeeId=2

I want to be able to do this on the client site, so I can't access eclipse or debugging tools, just look for JVM parameters, or make code changes I've seen them, but I can't find anything During this period, I will also continue to search;)

Solution

Given all your limitations, I cannot recommend anything other than JDB Fire on the bad boy and start line by line through the client code I know you said there are no debugging tools, but unless they are JRE environment, JDB should have been installed

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>