Home > Articles > Programming > Java

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

Customizing Breakpoint Behavior

There are a number of things that you can do to customize when a breakpoint is hit and what happens in the IDE when a breakpoint is hit. The following subtopics cover some of those things.

Logging Breakpoints without Suspending Execution

If you would like to monitor when a breakpoint is hit without suspending execution each time the breakpoint is hit, you can configure the breakpoint so that it does not cause suspension of execution. When such a breakpoint is hit in the code, a message is printed in the Debugger Console window.

To turn off suspension of execution when a breakpoint is hit:

  1. Open the Breakpoints window by choosing Window | Debugging | Breakpoints (Alt-Shift-5).
  2. In the Breakpoints window, double-click the breakpoint to open the Customize Breakpoint window. (For line breakpoints, right-click the breakpoint and choose Customize.)
  3. In the Action combo box, select No Thread (Continue).

Customizing Console Messages When Breakpoints Are Hit

You can customize the text that is printed to the console when a breakpoint is hit in your code.

To customize the console message that is printed when a breakpoint is reached:

  1. Open the Breakpoints window by choosing Window | Debugging | Breakpoints (Alt-Shift-5).
  2. In the Breakpoints window, double-click the breakpoint to open the Customize Breakpoint window. (For line breakpoints, right-click the breakpoint and choose Customize.)
  3. In the Print Text combo box, modify the text that you want printed.

To make the printed text more meaningful, you can use some substitution codes to have things like the thread name and the line number printed. See Table 5-6 for a list of the substitution codes available.

Table 5-6. Substitution Codes for Breakpoint Console Text

Substitution Code

Prints

{className}

The name of the class where the breakpoint is hit. This code does not work for thread breakpoints.

{lineNumber}

The line number at which execution is suspended. This code does not work for thread breakpoints.

{methodName}

The method in which execution is suspended. This code does not work for thread breakpoints.

{threadName}

The thread in which the breakpoint is hit.

{variableValue}

The value of the variable (for breakpoints set on variables) or the value of the exception (for exception breakpoints).

{variableType}

The variable type (for breakpoints set on variables) or the exception type (for exception breakpoints).

Making Breakpoints Conditional

You can set up a breakpoint to suspend execution of the code only if a certain condition is met. For example, if you have a long For loop, and you want to see what happens just before the loop finishes, you can make the breakpoint contingent on the iterator's reaching a certain value.

Here are some examples of conditions you can place upon a breakpoint:

  • i==4 (which means that the execution will stop on the breakpoint only if the variable i equals 4 in the current scope)
  • ObjectVariable !=null (which means that execution will not stop at the breakpoint until ObjectVariable is assigned a value)
  • MethodName (where Method has a Boolean return type and execution will stop at the breakpoint only if Method returns true)
  • CollectionX .contains( ObjectX ) (which means that execution will stop at the breakpoint only if ObjectX is in the collection)

To make a breakpoint conditional:

  1. Open the Breakpoints window by pressing Alt-Shift-5.
  2. In the Breakpoints window, right-click the breakpoint that you want to place a condition on and choose Customize.
  3. In the Customize Breakpoint dialog box, fill in the Condition field with the condition that needs to be satisfied for execution to be suspended at the breakpoint.

Conditional breakpoints are marked with the breakpoints.gif icon.

  • + Share This
  • 🔖 Save To Your Account