modeller
modeller

Reputation: 3850

In IntelliJ, when I trigger an exception, how do I look back and find which line threw it?

When I debug a block of Java code by stepping through it, some line throws an exception and I need to search for which line threw it.

But now I am already in the middle of the exception handler code. Is there a way to trace which line of code in my procedure that threw this exception?

I am using IntelliJ Idea 16.2.

Upvotes: 3

Views: 2832

Answers (3)

Renato
Renato

Reputation: 13700

If you have the exception in scope while in the debugger session, press Alt+F8 in Mac (Evaluate Expression), not sure other OS, then execute exception.printStacktrace() and you'll get clickable stack trace in the Console window (or just look at the stackTrace array in the class inspector).

Upvotes: 3

Diego Rojas
Diego Rojas

Reputation: 244

Put a breakpoint in your method that invokes this procedure, use F7 to move between lines in order of execution, F8 to go to the next breakpoint an F9 to end the method process. You can use a try catch block yo handle this exception and log in the console what type is and what line throw it.

Hope it helps.

Upvotes: 1

nasukkin
nasukkin

Reputation: 2540

There's a few ways. First of all, if you don't catch the exception at all, the JVM will barf it into the standard error stream (stderr) and your thread will terminate. If this happens, IntelliJ will print it in the output console (If you're using the Darkula theme, I believe that stderr comes out in red text) with a full stack-trace. IntelliJ also formats the output so that you can click on the line-numbers of each stack element and see exactly where the exception came from.

Alternatively, if you have caught the exception and want to handle it more gracefully but still want to output the line numbers, you can tell the Exception to print itself to a particular PrintStream (such as System.out or System.err) with the java.lang.Throwable#printStackTrace(java.io.PrintStream) method.

Now, if you're actually debugging the application (Stepping through it with breakpoints and whatnot), things are even easier and more detailed. IntelliJ's Debugger is excellent for viewing the whole stack (albeit a bit slow if you're debugging a remote application). It looks something like this: https://i.sstatic.net/de5wB.png

Up top, you have your source code. Down below you have your Debugger. The "Frames" pane is your call-stack; you can click on any method in the stack to view it AND all of its stack variables. With a few clicks, you should be able to see where your exception is being thrown from, just by navigating the stack frames and looking for the relevant throw statement.

BUT WAIT, THERE'S MORE! Do you know what exception you're looking for and want to break as soon as it's thrown? IntelliJ can do that! Click on the "View Breakpoints" button (off to the left side of the Frames pane) and you can add an Exception Breakpoint simply by specifying the name of the Exception class that you want to catch. As soon as the exception of that time is thrown, your debugger will pause and take you to it. This makes navigating to a particular Exception trivial.

Upvotes: 1

Related Questions