nitsua
nitsua

Reputation: 793

Java keyword to start debugger?

I'm curious if there's a keyword in Java which would allow eclipse or an IDE to stop running through code and start the debugger when the code reaches a particular point.

I understand that breakpoints are very useful for this particular problem, however I wish to only begin to debug the program if a particular condition is reached, and this condition is one which is not checked by the program regularly, so I'd have to program it in purely for debugging.

For example,

if(condition_is_met){
    //throw debugger and begin to step through here
}

The only other way I can think of doing it is...

if(condition_is_met){
    System.out.println("Something"); //Then set a breakpoint here in Eclipse.
}

Which just seems messy, and means I would be liable to miss it when cleaning up the code later. Does Java have a keyword for this, perhaps similar to Javascript's debugger keyword?

Upvotes: 1

Views: 2201

Answers (4)

Aaron Digulla
Aaron Digulla

Reputation: 328624

Create a utility class:

public final class DebugHelper {
    public final static void breakpoint() {} // Set break point here
}

You can then use DebugHelper.breakpoint() in all the places where you want Eclipse to stop.

At runtime, the method will eventually be replaced with nothing by the JIT (when debugging is disabled), so it's free for frequently used code.

This approach has two advantages:

  1. It doesn't hurt to leave this code in
  2. If you want to remove it, it's easy to find (just search for all references of the method in the workspace)

Upvotes: 4

sakthisundar
sakthisundar

Reputation: 3288

There is no such keyword in Java that would start the eclipse debugger . Remember Java is not for eclipse but it is the other way around. If you want such a thing, use conditional break points in eclipse which you can set through break point properties window of eclipse.

Upvotes: 2

Mark Rotteveel
Mark Rotteveel

Reputation: 109015

Eclipse has conditional breakpoints: Use the context menu on the breakpoint in the left editor margin or in the Breakpoints view in the Debug perspective, and select the breakpoint’s properties. In the dialog box, check Enable Condition, and enter an arbitrary Java condition, such as list.size()==0

(from: http://wiki.eclipse.org/FAQ_How_do_I_set_a_conditional_breakpoint%3F )

Upvotes: 1

Amit Deshpande
Amit Deshpande

Reputation: 19185

There is no keyword for it but you can specify conditional debugging in Eclipse.

Check below

  1. FAQ How do I set a conditional breakpoint?
  2. Effective Debugging: Conditional Breakpoints

Upvotes: 6

Related Questions