Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I debug Java code inside a "try {...}" block?

I (Java newbie) am running a Java program in Eclipse (project I downloaded from SourceForge). When I debug it, it always ends up at this location, which occurs after a 50-line try block:

catch (Exception ex){
    System.err.println("PROCESSING ERROR: " + ex.getMessage());
    helpFormatter.printHelp(usage, options);
}

How can I find and debug the line of code that triggers this exception? (i.e. see the line number, inspect local variables, evaluate expressions, etc.) If I place a breakpoint after the catch, I don't see any way to get back to the original error or at least find out the line number. I could step through the code in the try block, but there are a lot of loops, so that could get extremely time consuming.

How can I debug this?

like image 417
RexE Avatar asked Feb 20 '23 15:02

RexE


1 Answers

Have a look at the stacktrace. You can find inyside an Eclipse tab somewhere, or print it directly when you catch the exception:

catch (Exception ex){
    ex.printStackTrace();
}

Inspecting the stacktrace is the fast and most secure way to find your error quickly. Even if you set a breakpoint (or worst use some print to locate the last statement executed) you won't be able to determine exactly where the exception occurred. In fact the exception could have been raised a lot deeper inside your calls stack, not necessarily in the first method called after the breakpoint.

like image 68
Heisenbug Avatar answered Feb 22 '23 03:02

Heisenbug