2

I'm assuming that the reason the assert statement throws an AssertionError to stop your program is because an assumption about your code is wrong. The problem is, how am I supposed to debug my code by using debugging tools when the assert statement kills my java process?

EDIT: My question is different because the answer to the supposedly duplicate question doesn't answer mine.

9
  • Most IDEs allow you to break when an exception is thrown.. Commented Dec 1, 2017 at 2:10
  • You put break point before assert. Commented Dec 1, 2017 at 2:12
  • @Blorgbeard But in that case you could break on any exception. Why use assert - why not just throw any old exception? Commented Dec 1, 2017 at 2:23
  • @tsolakp That's not reliable because I'll only know where to put the breakpoint in hindsight AFTER the assertionerror has been thrown and my VM has suspended. In which case I will have to restart my program so I've lost all the config that caused that error. Commented Dec 1, 2017 at 2:28
  • 1
    Why use assert - why not just throw any old exception? Assertions can be disabled, allowing you to avoid the performance hit in production. Commented Dec 1, 2017 at 5:07

1 Answer 1

0

EDIT: If you look for a way to throw an exception and not throw an exception at the same time, there is no solution for this.

In modern IDEs you can, during debug:

  1. tell the IDE to block on any AssertionError without setting any breakpoint
  2. change the code of a method during a test when blocked, such as commenting out an assert
  3. tell the IDE to resume running a method again How to step back in Eclipse debugger?

Also assert can be disabled for the purpose of debugging. That's one reason to use an assert instead of an if-block.

See: How do I disable Java assertions for a junit test in the code

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.