How can I gather information about exception?

Since I work in project also in maintenance team I have pretty often deal with same exceptions which occurs pretty often (in the same place) but there are insufficient amount of information in stacktrace about them. Which causes situation where even if it’s very often I cannot do anything about them because cause is unknown.

So I was wondering if there are some tools which would allow to gather more information about context (probably by connecting somehow JVM).

Is this place where I should use these guys:
jinfo, jhat, jmap, jsadebugd, jstack?

Or maybe there are some more handy/more powerful/(call it) ways?

What I would expect:

  • dump values of variables (in invoked method and if possible in other methods in stacktrace)
  • solution doesn’t change code itself
  • checking if some other defined methods was invoked before exception occured

The stack trace provides you with the exact line of code where the problem occurred along with a (hopefully) useful message to explain the problem, which should be enough to identify the problem.

Generally, if it’s not, the best thing to do is to modify the code in the problematic area, to give you more information. That might be to log the state of variables just before the line where the exception occurs, in all cases (not just when there’s an exception), or to increase the amount of information provided when the exception does occur.

Alternatively, if there is more than one thing happening on the line where the exception occurs, you could split the code there into multiple lines, which will cause the line number in all subsequent exceptions to be more helpful, for example, if you have a line like this:

Foo myFoo = myBar.getBaz().getFoo();

and you get a NullPointerException on that line, you might not be able to identify whether myBar is null, or whether its getBaz() method returns null. If you split that into:

Baz myBaz = myBar.getBaz();
Foo myFoo = myBaz.getFoo();

the next time you encounter the same NullPointerException the line number will give you a better clue as to what is null.