390

Is there feature that will automatically break debugging on first exception occurrence?

So we

  1. start application
  2. do something that throw exception
  3. got IntelliJ popped up highlighted line where exception occurred.
Alex B
  • 24,678
  • 14
  • 64
  • 87
Mike Chaliy
  • 25,801
  • 18
  • 67
  • 105

6 Answers6

448

Run | View Breakpoints | Exception Breakpoints

Fabian Tamp
  • 4,416
  • 2
  • 26
  • 42
Mike Chaliy
  • 25,801
  • 18
  • 67
  • 105
192

A fast way to pop up the dialog is to press Ctrl + SHIFT + F8 (On Mac: Cmd + SHIFT + F8), then click over to the exception breakpoints tab. If that was the last tab you were viewing, it'll still be selected, making it easy to flick breaking on exceptions on and off.

This will cause IntelliJ to break at the point in the code (or library code) where the exception was raised. Specifically, you get a 'first chance' at exception handling, before the stack is walked looking for catch/finally blocks to execute.


TIP: Java tends to throw a lot of exceptions internally when loading classes, so this breaking on all exceptions can become quite tedious. The good news is that you can exclude certain types of exception using the condition field.

For example:

!(this instanceof java.lang.ClassNotFoundException)

You can chain multiple such conditions together with &&.

enter image description here

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • 25
    Is there a way to tell "break on exceptions from my project only (not from the Java system)"? – Damn Vegetables Feb 01 '16 at 16:34
  • 26
    Don't forget the "Disabled until selected breakpoint is hit:" -- this one is amazing. Just set a breakpoint in *your code* after all the Java or other libraries have been loaded and set up. Then pick your breakpoint from the dropdown and wait. Your breakpoint will be hit, and then once you hit continue, your new Caught & Uncaught Exception filter will kick in. :) – Louis St-Amour Jun 13 '16 at 05:45
  • 1
    Don't forget to check both "Caught exception" and "Uncaught exception" at the bottom of the Breakpoints dialog box. – Robino Dec 31 '17 at 10:11
  • @DamnVegetables You can set a couple of breakpoints where this Exception may occur and check "Disabled until selected breakpoint is hit:". See Louis St-Amour's comment below you. – fall Jul 15 '21 at 03:04
74

In IntelliJ IDEA 14 go to:

Run -> View Breakpoints -> Check "Java Exceptions Breakpoints" -> Uncheck "Caught Exceptions"

If you do not uncheck Caught Exceptions the execution will be stopped every time the Java Framework throws an internal exception.

Tonatio
  • 4,026
  • 35
  • 24
18

In newer versions of intellij, it is under Run > View Breakpoints.

Then you can check Java Exception Breakpoints -> Any Exception.

A good way to debug the exceptions is to use your main app package and the wild card .*. This way you skip all the other libraries exceptions, since most of the times you are looking for exceptions throwed by your app and not by any other library (which can be a lot of exceptions).

As an example showed in the image, I use com.gs.mercury.* to break every time the app throws an exception. If you use exceptions for what they are for (to handle exceptional cases and not to handle the flow of normal situations) you will only stop when you reach the desired exception almost all the time.

You would use Catch class filters to specify the classes of the exceptions you are expecting to catch, and Class filters to specify in which classes you expecing to catch the exception.

Note: Added the clarification between Catch class filters and Class filters made by @Roman in the comments.

PS. answer added just to point out the pretty useful Catch class filters and Class filters.

Breakpoints

Jose Da Silva Gomes
  • 3,814
  • 3
  • 24
  • 34
  • 1
    Note that "Catch class filters" sets a filter for the exception classes, but not for the code itself. use the "Class filters" field If you need to filter the scope of your code – Roman Feb 08 '23 at 17:13
12

Yes, there is. You need to define an exception breakpoint (it can be "Any exception") in the breakpoints dialog in IntelliJ IDEA.

The exceptions can be filtered by condition or class if desired, or by whether you are interested in caught or uncaught exceptions.

Avi
  • 19,934
  • 4
  • 57
  • 70
8

If you click on the little "+" sign in the upper left corner, you can add a new breakpoint. If you select Exception Breakpoint, you get a little dialog where you can enter the exception class to break on (in case you don't want to break on all exceptions).

koljaTM
  • 10,064
  • 2
  • 40
  • 42