In Java debugger, how to ignore exceptions that never go through my code

I am currently using IntelliJ IDEA for Java development, but I am also interested in answers targeting other IDEs or general concepts for debugging Java code. Since this is a feature that I missed in a number of IDEs, I'm not sure if I missed the concept of a workflow while transferring my debugging habits from other languages.

Say I'm writing code in myapp.* Using the framework classes from somelib.* . A typical stack trace can start in any packet and can switch between them several times. Let them also say that I am debugging the assumption that there are errors in my code and that there is no library in the code. An example of a stack trace (showing only class names):

 somelib.D (current stack frame) somelib.C myapp.Y myapp.X somelib.B somelib.A 

Usually, I am not interested in the following types of exceptions and do not want the debugger to break them into:

  • Thrown in somelib.B and caught in somelib.A . Either the library code throws exceptions to handle the problem state inside the library, or to stop the application. In the latter case, I am interested in the exception message, which I hope tells me what is wrong.

  • Thrown in somelib.D and caught in somelib.C . The library code can use exceptions as a form of logic in which a certain action is used, and an alternative route is taken in case of a problem or when my code is notified about the problem in other ways (for example, returns a null reference if necessary).

Types of exceptions that interest me:

  • It is somelib.C into somelib.C or somelib.D and misses into somelib.C or somelib.D . Here I want the debugger to myapp.Y into a line in myapp.Y , where I call the code from somelib.C .

  • Thrown in myapp.X or myapp.Y , either caught or not displayed. Here I want the debugger to break into the line where the exception was sent.

IntelliJ IDEA gives me choices, I want to break the caught or uncaught exceptions, or both, and limit the place where the exception is selected for a set of classes. These options do not help, since I usually want to catch any exception, to be caught or not displayed, while the code that I wrote is between the place that he chose and the place that he caught in the end.

+4
java debugging intellij-idea exception
source share
4 answers

It seems impossible to do exactly what I wanted in a convenient way. During this question, I studied Java and taught it to my students, so a simple solution would be favorable.

But with my current experience, it seems reasonable to use the following approach:

  • Place a catch (Trowable t) around each body of the thread, that is, at the top level of Runnable.run() and static main(String[]) , and write these “uncaught” exceptions to the console.
    • Care must be taken not to accidentally catch checked exceptions using these "catchy" handlers, but to process them as part of the program logic.
  • When starting debugging, do not create exception control points.
  • Once an exception is logged in the console, create an exception checkpoint for it and try to force it to reset again.
    • This should only happen without exceptions (i.e. subclasses of RuntimeException ), since other exceptions must be explicitly caught and handled in any case.
    • Since catching and handling thrown exceptions is usually not part of the program logic, creating these exception control points without restrictions on the place it throws should not catch any “false positives”.

An alternative is to skip the last two steps and instead create an exception RuntimeException for a RuntimeException . But it has a higher change in catching exceptions that are caught as part of the program logic.

0
source share

You can create two breakpionts exceptions:

  • An exception breakpoint that fires whenever there is an exception in myapp.* (Caught or unmapped). Use a class filter for this purpose (see here for class filters).
  • An exceptional breakpoint that fires whenever there is an uncaught exception in "somelib.C1 or somelib.B`. Use class filters again to limit the breakpoint.
+1
source share

In IntelliJ, you can define condition at the breakpoint of exception . The condition is only Java code that executes when the breakpoint is hit, so the exception object will be in scope during condition checking. You can call getStackTrace() to throw an exception and decide if this is the exception that interests you by looking at each frame on the stack. If you are not interested, your state fails and the breakpoint is skipped.

+1
source share

Use Eclipse! Using Eclipse, you can filter where the exception should stop debugging using breakpoint properties.

-2
source share

All Articles