![]() |
< Day Day Up > |
![]() |
Recipe 5.11 Creating Field, Method, and Exception Breakpoints5.11.1 ProblemYou need to stop execution when a field or method is about to be accessed, or when a certain exception occurs. 5.11.2 SolutionSet a field, method, or exception breakpoint using the appropriate items in the Run menu. 5.11.3 DiscussionThe 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 breakpointsField 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 Figure 5-18. Setting watchpoint properties![]() 5.11.3.2 Method breakpointsMethod 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 Figure 5-19. Setting up a method breakpoint![]() 5.11.3.3 Exception breakpointsYou 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 Figure 5-20. Setting up an exception breakpoint![]() 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![]() 5.11.3.4 Eclipse 3.0The 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 AlsoRecipe 5.10 on configuring breakpoint conditions; Chapter 3 of Eclipse (O'Reilly). |
![]() |
< Day Day Up > |
![]() |