39

I'd like to run my test suite in the debugger and break on any unexpected exception, but the Java classloaders throw lots of ClassNotFoundExceptions during normal operation.

So it would be nice if I could create an exception breakpoint that ignores ClassNotFoundExceptions and stops on everything else.

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
Dan Berindei
  • 7,054
  • 3
  • 41
  • 48
  • 1
    What you want to accomplish is best achieved by adding a class filter of the form `com.mypackage.*` to `Any Exception` – Mike Nakis Jul 12 '13 at 22:29

3 Answers3

61

This answer is almost the same as that of Mindas, but the details were enough for me to ignore his suggestion the first time around, and bother the Intellij support guys/girls (thanks Serge and Eugene):

  • Open the 'Breakpoints' window and go to the 'Exception Breakpoints' tab
  • Highlight and activate the 'Any exception' breakpoint
  • Activate only the 'Condition' Condition and type in the following:

    !(this instanceof java.lang.ClassNotFoundException)
    

IDEA will remove 'java.lang' immediately (version 11.01), but it is required for this solution to work. If you don't use that, you will get a ClassNotFound popup box (irony oh irony).

I did find out that a lot of 'standard' libraries throw exceptions in their normal flow of operations. When you successfully ignore the ClassNotFoundException's, you will find that others suddenly appear. Nothing is ever easy.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
Jan
  • 808
  • 8
  • 19
  • 2
    `this` is the key, apparently! While testing it IDEA didn't remove the 'java.lang' bit, but it did put red squiggles under the expression and warned me that `this` cannot be converted to ClassNotFoundException. I just ignored it and it worked perfectly. – Dan Berindei Jan 31 '12 at 11:55
  • Possibly fixed in IntelliJ 12, but as of IntelliJ 13/Android Studio, this works perfectly! Really glad I found this tip. – Louis St-Amour Nov 20 '13 at 16:19
6

For some reason I kept getting "failed to evaluate breakpoint expression" when I put the !ClassNotFoundException condition under the "any exception" breakpoint rules. I was able to work around it by creating a custom "any exception" breakpoint item though:

  1. Open the breakpoints window from the debug view.
  2. Uncheck "Any Exception" so we will not be stopping at any exception using the default item.
  3. Click the + sign, click java exception breakpoints, check "Include Non-Project Classes", type "java.lang.Exception" in the box, select the result that shows up in the box.
  4. Select the Exception item that is generated in the list (under "Any Exception"), and put

    !(this instanceof java.lang.ClassNotFoundException) 
    

    in the condition box.

ceph3us
  • 7,326
  • 3
  • 36
  • 43
adavea
  • 1,535
  • 1
  • 19
  • 25
1
  • right click on the breakpoint, and click on Properties
  • go to Conditions pane
  • tick a checkbox at Condition
  • type !(myException instanceof ClassNotFoundException)
mindas
  • 26,463
  • 15
  • 97
  • 154
  • It's an exception breakpoint, so I can't right click on it... – Dan Berindei Jun 20 '11 at 11:00
  • 1
    I tried your suggestion anyway, hoping there is an implicit variable called `myException` usable in exception breakpoint conditions, but it doesn't work. – Dan Berindei Jun 20 '11 at 11:01
  • Can you not make it a Line Breakpoint? `myException` wasn't meant to be interpreted literally but as an exception you catch. Anyway, you can define class filter with Exception Breakpoints which should do a similar thing. – mindas Jun 20 '11 at 11:12
  • 1
    I realized `myException` was just a placeholder, but I was hoping you knew something that I didn't :) I guess I could put a line breakpoint in each of Throwable's constructors, but that be a little inconvenient in that I'd have to set the condition 4 times. AFAIK the class filters for Exception breakpoints are for the class the exception is thrown from, not for the class of the exception (and they rarely work, at least for me). – Dan Berindei Jun 20 '11 at 13:46
  • Yeah, you're right (and I'm wrong) regarding class filter. Anyway, I don't think there is a way to achieve what you want using Exception Breakpoints, but you can always report an improvement request to IntelliJ guys :) – mindas Jun 20 '11 at 15:48
  • Has this been fixed by JetBrains? We need an option to disable stopping at JDK exceptions. – WestCoastProjects Sep 11 '13 at 03:08