Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

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

Breakpoint Type

Description

Line

Set on a line of code. When the debugger reaches that line, it stops before executing the line. The breakpoint is marked by pink background high-lighting and the breakpoint.gif icon. You can also specify conditions for line breakpoints.

Class

Execution is suspended when the class is referenced from another class and before any lines of the class with the breakpoint are executed.

Exception

Execution is suspended when an exception occurs. You can specify whether execution stops on caught exceptions, uncaught exceptions, or both.

Method

Execution is suspended when the method is called.

Variable

Execution is suspended when the variable is accessed. You can also configure the breakpoint to have execution suspended only when the variable is modified.

Thread

Execution is suspended whenever a thread is started or terminated. You can also set the breakpoint on the thread's death (or both the start and death of the thread).

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:

  1. Choose Run | New Breakpoint (Ctrl-Shift-F8).
  2. In the New Breakpoint dialog box, select Class from the Breakpoint Type combo box.
  3. 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:

  1. Choose Run | New Breakpoint (Ctrl-Shift-8).
  2. In the New Breakpoint dialog box, select Method from the Breakpoint Type combo box.
  3. 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:

  1. Choose Run | New Breakpoint (Ctrl-Shift-F8).
  2. In the New Breakpoint dialog box, select Exception from the Breakpoint Type combo box.
  3. In the Exception Class Name field, select the type of exception that you would like to set the breakpoint on.
  4. 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:

  1. Choose Run | New Breakpoint (Ctrl-Shift-F8).
  2. In the New Breakpoint dialog box, select Variable from the Breakpoint Type combo box.
  3. Fill in the Package Name, Class Name, and Field Name fields.
  4. Select an option from the Stop On combo box.
  5. 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:

  1. Choose Run | New Breakpoint (Ctrl-Shift-F8).
  2. In the New Breakpoint dialog box, select Thread from the Breakpoint Type combo box.
  3. In the Set Breakpoint On field, select Thread Start, Thread Death, or Thread Start or Death.
  • + Share This
  • 🔖 Save To Your Account