3

I used abort() In C to make my program crash. And debugger will trap this.

Maybe equivalent in Java should be exception. But Java also has very strict exception chaining policy, so using of exception for debugging purpose doesn't seem to be a good idea.

What should I use for this in Java? Intentional program crash for debugging purpose which can be trapped by debugger. (I am using Eclipse)

Update

It would be very nice if the code can be optionally opt-out on release build.

eonil
  • 83,476
  • 81
  • 317
  • 516

3 Answers3

4

In Eclipse you can set debug points, and run in debug mode.

That said, there is a difference between a CheckedException (which must be caught by the calling function or passed up the chain) and an UncheckedException (which is closer to what you want). Throw the latter if you want to force a crash.

However, this is generally a bad way to program. You'll find better results using alternative means to catch the errors you want.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
2

You can use assert(false). Assuming you're using Eclipse for the debugger, you can set Eclipse to break on specific exceptions (in this case, AssertionError). Make sure you enable assertions, otherwise it won't do anything.

Since you have to explicitly tell the JVM to enable assertions, I think this would count as "code [that] can be optionally opt-out on release build."

Community
  • 1
  • 1
DaoWen
  • 32,589
  • 6
  • 74
  • 101
0

I do not know how to handle it in Eclipse. However, under JDB, this can be done by catching the Exceptions. Eclipse should have such an interface.

Using command line like this

"catch java.io.FileNotFoundException"

For details, please refer to here

Kun Ling
  • 2,211
  • 14
  • 22