Recipe 5.11 Creating Field, Method, and Exception Breakpoints

     

5.11.1 Problem

You need to stop execution when a field or method is about to be accessed, or when a certain exception occurs.

5.11.2 Solution

Set a field, method, or exception breakpoint using the appropriate items in the Run menu.

5.11.3 Discussion

The standard breakpoints we've been using are called line breakpoints ; besides line breakpoints, the JDT supports three other types of breakpoints: field, method, and exception breakpoints.

5.11.3.1 Field breakpoints

Field breakpoints , also called watchpoints , suspend execution when your code is going to access and/or modify the value of a field (watchpoints cannot be set on local variables , only on fields). This is different from when you set a condition on a standard breakpoint; this breakpoint happens every time a field is going to be accessed in any way. Using a watchpoint is often much easier than trying to catch all the possible points in your code in which the field you want to watch might be modified.

To set a watchpoint, highlight a field in a Java view and select Run Add/Remove Watchpoint. The new watchpoint will appear in the Breakpoints view, and you can configure it by right-clicking it and then clicking Breakpoint Properties, which opens the Java Watchpoint Properties dialog shown in Figure 5-18. Note in particular that you can check two checkboxes here"Access" and "Modification"that enable you to indicate whether you want to suspend execution when the field is accessed and/or when it is modified.

Figure 5-18. Setting watchpoint properties
figs/ecb_0518.gif

5.11.3.2 Method breakpoints

Method breakpoints suspend execution when you enter or leave a method, depending on how you configure the breakpoints. You usually use these breakpoints on methods for which you do not have source code. To set a method breakpoint, highlight the call to that method in a Java view, and select Run Add/Remove Method Breakpoint. You can configure these breakpoints by right-clicking them in the Breakpoints view, which opens the Java Method Breakpoint Properties dialog shown in Figure 5-19. You can select whether the breakpoint happens on entry into the method, exit from the method, or both, using the checkboxes in this dialog.

Figure 5-19. Setting up a method breakpoint
figs/ecb_0519.gif

5.11.3.3 Exception breakpoints

You also can work with exception breakpoints , which enable you to suspend execution when an exception occurs. This is very useful if your code throws unexpected exceptions, such as a NullPointerException . You can suspend execution and see what's going on with your code when a thrown exception is caught or not caught.

To set an exception breakpoint, select Run Add/Remove Exception Breakpoint, which opens the dialog shown in Figure 5-20 that enables you to select which breakpoint(s) you are interested in, as well as whether you want to break when they are caught or not caught, or both.

Figure 5-20. Setting up an exception breakpoint
figs/ecb_0520.gif

You can configure the properties for an exception breakpoint as you can for any other breakpoint. Just right-click the breakpoint in the Breakpoints view, and click Properties. For example, you can see how to configure an exception breakpoint for uncaught java.lang.NullPointerException exceptions in Figure 5-21. You can restrict the breakpoint to specific locations, as shown in Figure 5-21, and even use hit counts.

Figure 5-21. Setting properties for a Java exception breakpoint
figs/ecb_0521.gif

5.11.3.4 Eclipse 3.0

The process for creating breakpoints in Eclipse 3.0 is the same as in earlier versions. However, the Add/Remove Watchpoint menu item is called Toggle Watchpoint in Eclipse 3.0. In addition, the breakpoint properties dialogs are laid out differently (but so far they contain the same items).

5.11.4 See Also

Recipe 5.10 on configuring breakpoint conditions; Chapter 3 of Eclipse (O'Reilly).



Eclipse Cookbook
Inside XML (Inside (New Riders))
ISBN: 596007108
EAN: 2147483647
Year: 2006
Pages: 232
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net