Although it's trivial to put a breakpoint in the catch
block, this is merely the location where the failure was ultimately caught, not where it was caused. The place where it was caught can often be in a completely different plug-in from where it was raised, and depending on the amount of information encoded within the exception (particularly if it has been transliterated into a different exception type), it may hide the original source of the problem. Fortunately, Eclipse can handle such cases with a Java Exception breakpoint.
Introduce a bug into the
SampleHandler
class'sexecute()
method, by adding the following just before theMessageDialog.openInformation()
call:window = null;
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Click on the Hello World icon.
Nothing will appear to happen in the target Eclipse, but in the Console view of the host Eclipse instance, the following error message should be seen:
Caused by: java.lang.NullPointerException at com.packtpub.e4.hello.ui.handlers.SampleHandler.execute(SampleHandler.java:30) at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:293) at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:76)
- Create a Java Exception breakpoint in the Breakpoints view of the debug perspective. The exception dialog will be shown as follows:
Enter
NullPointerException
into the search dialog and click on OK.Click on the Hello World icon and the debugger will stop at the line the exception is thrown, instead of where it is caught:
The Java Exception breakpoint stops when an exception is thrown, not when it is caught. The dialog asks for a single exception class to catch and by default, the wizard has been prefilled with any class whose name includes *Exception*
. However, any name (or filter) can be typed into the search box, including abbreviations such as FNFE
for FileNotFoundException
. Wildcard patterns can also be used, which allows searching for Nu*Ex
or *Unknown*
.
By default, the exception breakpoint corresponds to instances of that specific class. This is useful (and quick) for exceptions such as NullPointerException
, but not so useful for ones with an extensive class hierarchy such as IOException
. In this case, there is a checkbox visible in the Breakpoint properties window and the bottom of the Breakpoints view, which allows the selection of all subclasses of that exception, not just of the specific class itself.
There are also two other checkboxes, which say whether the debugger should stop when the exception is caught or uncaught. Both of these are selected by default; if both are deselected, the breakpoint effectively becomes disabled. Caught means that the exception is thrown in a corresponding try
/catch
block, and Uncaught means that the exception is thrown without a try
/catch
block (thus, bubbles up to the method's caller).