tags:

views:

63

answers:

2

I'm sorry if the question title may mis-inform you, but I cannot find a proper word to explain that.

If you ever working with .NET, you would know that there is Assert class that will automatically wake up and attach debugger if necessary and then have debugger (Visual Studio) pausing at the Assert command, given the Assert command failed.

Given I'm running a java program and having debugger connected, then how can I have debugger to break on certain condition without manually setup a break point?

I'm expecting something like that:

void doSomeThing(String x){
   if (x==null) breakDebuggerNow();
}
+1  A: 

If you use eclipse you can modify breakpoints in the breakpoint view (per default in debug perspective) by the context menu (right click on your break point). You can set Hit Count, or Enable Condition. Another option is to trigger on exceptions, click the icon with a 'J!' set select a specific exception.

stacker
Conditional break points is a very handy feature
Guillaume
+1  A: 

As far as I know there is no exactly such feature in Java. JVM doesn't know about debugger if it is not attached. For .Net this is Windows who does some favor in this aspect. However, there is assert that is intended for such usage. It will raise java.lang.AssertionError that is not an Exception and thus shouldn't be handled by usual catches. If your debugger is set up to handle unhandled exceptions (or rather Throwables) it will catch such case.

iPhone beginner
Hmm, look like I'm coming close. But isn't that AssertionError can also be handled by Eclipse? I'm finding that Eclipse can also hook on AssertionError.
Phương Nguyễn
Yes, any good enough Java-debugger can handle some specific exceptions and/or errors and Eclipse definitely has good enough debugger. I can't give you a good advice on how exactly you should configure Eclipse as I'm used to Idea, but I don't think it is hard to do. My point was that in Java you have to have debugger already attached to the application to handle assertion failures and should expect a slightly different thing (`AssertionError`). But in other aspects this is quite the same feature as in .Net world.
iPhone beginner