- Starting a Debugging Session
- Attaching the Debugger to a Running Application
- Starting the Debugger Outside of the Project's Main Class
- Stepping Through Code
- Setting Breakpoints
- Managing Breakpoints
- Customizing Breakpoint Behavior
- Monitoring Variables and Expressions
- Backing Up from a Method to Its Call
- Monitoring and Controlling Execution of Threads
- Fixing Code During a Debugging Session
- Viewing Multiple Debugger Windows Simultaneously
Setting Breakpoints
A breakpoint is a marker that you can set to specify where execution should pause when you are running your application in the IDE's debugger. Breakpoints are stored in the IDE (not in your application's code) and persist between debugging sessions and IDE sessions.
When execution pauses on a breakpoint, the line where execution has paused is highlighted in green in the Source Editor, and a message is printed in the Debugger Console with information on the breakpoint that has been reached.
In their simplest form, breakpoints provide a way for you to pause the running program at a specific point so that you can
- Monitor the values of variables at that point in the program's execution.
- Take control of program execution by stepping through code line by line or method by method.
However, you can also use breakpoints as a diagnostic tool to do things such as:
- Detect when the value of a field or local variable is changed (which, for example, could help you determine what part of code assigned an inappropriate value to a field).
- Detect when an object is created (which might, for example, be useful when trying to track down a memory leak).
You can set multiple breakpoints, and you can set different types of breakpoints. The simplest kind of breakpoint is a line breakpoint, where execution of the program stops at a specific line. You can also set breakpoints on other situations, such as the calling of a method, the throwing of an exception, or the changing of a variable's value. In addition, you can set conditions in some types of breakpoints so that they suspend execution of the program only under specific circumstances. See Table 5-5 for a summary of the types of breakpoints.
Table 5-5. Breakpoint Categories
Setting a Line Breakpoint
To set a line breakpoint, click the left margin of the line where you want to set the breakpoint or click in the line and press Ctrl-F8.
To delete the breakpoint, click the left margin of the line or click in the line and press Ctrl-F8.
If you want to customize a line breakpoint, you can do so through the Breakpoints window. Choose Window | Debugging | Breakpoints or press Alt-Shift-5. In the Breakpoints window, right-click the breakpoint and choose Customize.
Setting a Breakpoint on a Class Call
You can set a breakpoint on a class so that the debugger pauses when code from the class is about to be accessed and/or when the class is unloaded from memory.
To set a breakpoint on a class:
- Choose Run | New Breakpoint (Ctrl-Shift-F8).
- In the New Breakpoint dialog box, select Class from the Breakpoint Type combo box.
- Enter the class and package names. These fields should be filled in automatically with the class currently displayed in the Source Editor.
Setting a Breakpoint on a Method or Constructor Call
You can set a breakpoint so that the debugger pauses when a method or constructor is called before any lines of the method or constructor are executed.
To set a breakpoint on a method or constructor:
- Choose Run | New Breakpoint (Ctrl-Shift-8).
- In the New Breakpoint dialog box, select Method from the Breakpoint Type combo box.
- Enter the class, package, and method names. These fields are filled in automatically according to the class open in the Source Editor and the location of the insertion point.
You can make the breakpoint apply to all methods and constructors in the class by checking the All Methods for Given Classes checkbox.
Setting a Breakpoint on an Exception
You can set a breakpoint so that the debugger pauses when an exception is thrown in your program.
To set a breakpoint on an exception:
- Choose Run | New Breakpoint (Ctrl-Shift-F8).
- In the New Breakpoint dialog box, select Exception from the Breakpoint Type combo box.
- In the Exception Class Name field, select the type of exception that you would like to set the breakpoint on.
- In the Stop On combo box, select whether you want the breakpoint to apply to caught exceptions, uncaught exceptions, or both.
Setting a Breakpoint on a Field or Local Variable
You can set a breakpoint so that the debugger pauses when a field or variable is accessed (or only when the field or variable is modified).
To set a breakpoint on a field or variable:
- Choose Run | New Breakpoint (Ctrl-Shift-F8).
- In the New Breakpoint dialog box, select Variable from the Breakpoint Type combo box.
- Fill in the Package Name, Class Name, and Field Name fields.
- Select an option from the Stop On combo box.
If you select Variable Access, execution is suspended every time that field or variable is accessed in the code.
If you select Variable Modification, execution is suspended only if the field or variable is modified.
Setting a Breakpoint on the Start or Death of a Thread
You can monitor the creation or death of threads in your program by setting a breakpoint to have execution suspended every time a new thread is created or ended.
To set a breakpoint on threads:
- Choose Run | New Breakpoint (Ctrl-Shift-F8).
- In the New Breakpoint dialog box, select Thread from the Breakpoint Type combo box.
- In the Set Breakpoint On field, select Thread Start, Thread Death, or Thread Start or Death.