0

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.

Ogen
  • 6,499
  • 7
  • 58
  • 124
  • Most IDEs allow you to break when an exception is thrown.. – Blorgbeard Dec 01 '17 at 02:10
  • You put break point before assert. – tsolakp Dec 01 '17 at 02:12
  • @Blorgbeard But in that case you could break on any exception. Why use assert - why not just throw any old exception? – Ogen Dec 01 '17 at 02: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. – Ogen Dec 01 '17 at 02:28
  • Are you saying you don't know where the assertion error is coming from? – tsolakp Dec 01 '17 at 02:29
  • @tsolakp I'm saying I can only add the breakpoint before the assert statement once it's already happened. – Ogen Dec 01 '17 at 02:30
  • Possible duplicate of [How do I disable Java assertions for a junit test in the code](https://stackoverflow.com/questions/28901375/how-do-i-disable-java-assertions-for-a-junit-test-in-the-code) – tkruse Dec 01 '17 at 04:50
  • @tkruse That's not a duplicate at all. It's got nothing to do with what I'm asking. – Ogen Dec 01 '17 at 05:02
  • *Why use assert - why not just throw any old exception?* Assertions can be disabled, allowing you to avoid the performance hit in production. – shmosel Dec 01 '17 at 05:07

1 Answers1

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

tkruse
  • 10,222
  • 7
  • 53
  • 80