1

I'm stepping through some code with this kind of structure:

try {
  doSomething();
} finally {
  doAnotherThing();
  // more nested try statements down here
} 

An exception is being triggered somewhere but I don't know if it's in doSomething(). Because the code goes to the doAnotherThing() regardless of whether an exception was triggered, that tells me nothing.

Is there a visual indication somewhere when an exception is triggered? (IntelliJ 14.1.7).

Steve Bennett
  • 114,604
  • 39
  • 168
  • 219

1 Answers1

2

Have a look under the Run -> Breakpoints menu item. You can add (using the '+' button), 'Java Exception Breakpoints' and specify the type of exception you want to break on. After it is added, you can use 'Disabled until selected breakpoint is hit' with another breakpoint inside your try block to restrict it to the code you care about. The Breakpoints window contains a lot of useful controls for when the breakpoint is triggered.

If you need to know if an exception was thrown, it will need to be stored in code:

Throwable thrown = null; 
try {   
    //Do very important work here
} catch (ImportantException e) {
    thrown = e;
    throw e;
} finally {   
    if (thrown != null) {
        //We arrived in the finally block due to an exception
    }
}
Alex Taylor
  • 8,343
  • 4
  • 25
  • 40
  • Thanks, that's very helpful. Is there a direct way to see "there is currently an exception being handled." – Steve Bennett Jul 10 '18 at 06:03
  • 1
    Unfortunately no. The closest you will get is following the methods listed in https://stackoverflow.com/questions/184704/is-it-possible-to-detect-if-an-exception-occurred-before-i-entered-a-finally-blo – Alex Taylor Jul 10 '18 at 06:14