163

It seems my Android Studio does not want to break on any exception by default. Enabling break on "Any Exception" starts breaking within actual JDE libraries. Is there any way to force it to break only on exceptions within my code only?

Coming from Visual Studio universe, looking for the default VS debug behavior here.

3 Answers 3

243

To break on all exceptions, caught or uncaught:

  1. Open the Breakpoints window via Run -> View Breakpoints.
  2. The Breakpoints dialog appears. In the left pane, scroll to the bottom. Select Any exception under Java Exception Breakpoints
  3. With Any exception selected, on the right pane, configure as follows:
    • Suspend: checked
    • All: selected
    • Condition: !(this instanceof java.lang.ClassNotFoundException)
    • Notifications: both Caught exception and Uncaught exception selected

Breakpoints dialog

  1. Define filters that specify namespaces of libraries that the debugger should break on: Check the Class filters checkbox to enable class filtering (as mentioned by @Scott Barta). Then click the ... (elipsis) button to open the Class Filters dialog. Specify class namespace patterns by clicking on the Add Pattern (Add Pattern) button. Enter:
    • com.myapp.* (replace this with the namespace prefix of your app)
    • java.* (note: as per OP's question, leave this out to NOT break on Java libraries)
    • android.* (as above, leave out to just debug own app code)
    • Add any additional namespaces as necessary (e.g. 3rd party libraries)

Class Filters

  1. Press OK, then dismiss the Breakpoints dialog.
5
  • 7
    @AndroidDev It's not the nicest interface, especially when coming from using the Visual Studio IDE. The class filters are to prevent all sorts of low-level errors from stopping the application much more frequently (especially during app initialization). As an example, follow the steps above, but without the class filters, then start the app in debug mode and see what happens.
    – CJBS
    Jun 5, 2015 at 15:58
  • 6
    @AndroidDev: I think it makes sense actually. By default it does break on all exception. You don't have to set conditions and filters. However, the OP did ask specifically for filtering of exceptions (exceptions in his own code). Jul 1, 2015 at 2:33
  • 6
    As an xcode/iOS developer and android-n00b, I didn't know that in order for ANY breakpoints to fire, in Android studio you cannot click the play-like (triangle) icon to start the app. You must start the app by clicking the bug-like icon next to it on the right.
    – xaphod
    Apr 13, 2016 at 13:41
  • @xaphod Another way is to run in release mode (press the play button), then once the app has started, attach the debugger (by pressing the device with the bug - portrait rectangle with green bug in lower right of icon), then select the process to be debugged. This is much faster if not debugging any of the app start-up code. See: developer.android.com/tools/debugging/…
    – CJBS
    Apr 13, 2016 at 16:52
  • 1
    If you're using junit (debugging while unit testing), you might want to add the class filter org.junit.* Jun 6, 2017 at 17:40
36

If you open up the Breakpoints window, it gives you quite a few options to have it conditionally break or not. What you're looking for is the "Class filters" here -- you can specify a wildcard expression with, for example, a Java package path, and it will only break for exceptions generated from matching classes.

5
  • 4
    For anyone looking for the Breakpoints window, it can be found in the menu, Run -> View Breakpoints.
    – Patrick
    Jan 25, 2015 at 22:02
  • 2
    I am trying this, but can't get it to work. What should the class filter be? Could give some examples? Feb 23, 2015 at 19:20
  • 2
    I'm in the same boat - the class filters just don't seem to work for me. I've tried com.mycompany.* but none of the exceptions cause it to break.
    – Kylotan
    Mar 18, 2015 at 12:55
  • 1
    It just stops in ZygoteInit.java everytime! Pretty useless! How to overcome that??? Jun 18, 2015 at 14:59
  • Run->View Breakpoints.... yeah, we wouldn't want a window to be displayed under the "View" menu, would we!
    – LarryBud
    Mar 25, 2018 at 15:44
7

To break on all exceptions in your code and other exceptions if uncaught:

This methods filters out the exception types that the runtime throws during normal operation (not very exceptional, are they?). It doesn't use the class filter, since it would filter out too much; bugs in your code often cause runtime classes to throw exceptions (e.g. accessing an array list past the end).

  1. Enable Java Exception BreakPoints / Any exception for uncaught exceptions only.

  2. Add a new Java Exception BreakPoint for the Exception (java.lang) class for caught and uncaught exceptions. Enable Condition and set it to this:

        !(this instanceof java.lang.ClassNotFoundException || this instanceof android.system.ErrnoException || this instanceof java.io.FileNotFoundException || this instanceof javax.net.ssl.SSLHandshakeException || this instanceof javax.net.ssl.SSLPeerUnverifiedException || this instanceof android.system.GaiException || this instanceof java.net.SocketTimeoutException || this instanceof java.net.SocketException || this instanceof java.security.NoSuchAlgorithmException)
    

Add to the exclusion list in the condition any other non-exceptional exceptions you encounter. (BTW, using java.lang.Exception is a way of effectively getting a second "Any exception" entry.)

1
  • not better to use !( a || b || c ) instead of !(a) && !(b) && !(c)
    – ceph3us
    Jun 20, 2016 at 10:22

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.