Héctor Ramos
Héctor Ramos

Reputation: 9252

Catching exceptions as an expression while debugging Java in Eclipse IDE

An everyday debugging situation for Java developers is that in which an Exception is thrown and then you need to dig into the debugger to find out what threw it. Usually you would try to set up some breakpoints before the exception is thrown and hope that you are able to determine the situation that leads up to that exception.

In Eclipse, a breakpoint may have an expression defined where it is only triggered when, for example, variable x equals value y. My question is, is it possible to define some kind of global expression where, once an exception is thrown, it is caught by the debugger, allowing the programmer to inspect all variables immediately? Ideally you would not have hit the catch block yet, Eclipse would catch the exception being raised and stop execution without changing the stack contents.

Is this possible or is it limited by the JVM?

Upvotes: 21

Views: 11257

Answers (2)

VonC
VonC

Reputation: 1328972

Another illustration: Eclipse Tip: Breakpoint on Exception

Eclipse let you set breakpoints based on where an Exception occurs.
You access the option via the "j!" http://help.eclipse.org/juno/topic/org.eclipse.jdt.doc.user/images/org.eclipse.jdt.debug.ui/elcl16/exc_catch.png icon in the debugging window (i.e., in the "Breakpoint View").

Add Java exception Window

The official help topic "Add Java Exception Breakpoint " has more on this.

  • The Uncaught Exception option is to suspend execution when an exception of the same type as the breakpoint is thrown in an uncaught location.
  • The Caught Exception option is to suspend execution when an exception of the same type as the breakpoint is thrown in a caught location.
  • do not forget the Exception Breakpoint Suspend on Subclass of this Exception:
    to suspend execution when subclasses of the exception type are encountered.
    For example, if an exception breakpoint for RuntimeException is configured to suspend on subclasses, it will also be triggered by a NullPointerException.

alt text

Upvotes: 37

TofuBeer
TofuBeer

Reputation: 61546

http://agile.csc.ncsu.edu/SEMaterials/tutorials/eclipse-debugger/

Specifically the "5.0 Special Breakpoints and Watching Expressions" portion.

Upvotes: 3

Related Questions