Using Java Assertions? Use an Assertion-failed Breakpoint!
Join the DZone community and get the full member experience.Join For Free
i'm a big fan of assertions . i use them very often in my code as safeguards. i was very pleased when true, native assertions were introduced to java: they allow adding more tests while keeping the code running faster when disabled.
if you like assertions, you will appreciate the following tip. a failed assertion will throw a java.lang.assertionerror runtime exception. logging the assertion failures is up to the application code. if your code does not report this error properly, you may completely miss the assertion failures.
eclipse has a very useful mechanism for breaking on exceptions . it's a different kind of breakpoint, which is triggered when the exception is thrown, regardless of how (and if) it is caught. if you are using assertions, i highly recommend setting a breakpoint on assertion failures when debugging your code. here's how:
- from the run menu, select add java exception breakpoint...
- type java.lang.assertionerror in the dialog box, select the exception and click ok.
that's it, you are done. from now on, the debugger will stop whenever an exception is throw. the breakpoint will appear in the breakpoints view. you can right click it and select breakpoint properties... to see all the settings you can control.
Opinions expressed by DZone contributors are their own.