Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Understand "assert"

 
Rajdeep Biswas
Ranch Hand
Posts: 224
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried to figure out the meaning of assert keyword, but even after reading few trusted resources, I am yet to come up with detailed clarification like why use it when it requires clients to run with -ea explicitly and how it performs in terms of exception handling. I need a sound answer, as I am a Java kid. Please explain it all. Thanks a lot just for reading.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Assert" is basically just a development and QA sanity check. It's not intended to be part of the normal running of a program, and correct behavior must not rely on it. I don't use it very often, but when I do, it's just to make sure that things that I think must be true are in fact true, in case I missed something or had a mistake in my logic.
 
Rajdeep Biswas
Ranch Hand
Posts: 224
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is it used as compared to handling or throwing RuntimeException in the program?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajdeep Biswas wrote:How is it used as compared to handling or throwing RuntimeException in the program?


A RuntimeException, such as, say, NullPointerException, is expected in a production release of your app if the relevant problem occurs. If you try to dereference a null pointer, it would be incorrect behavior to NOT throw NPE.

In contrast, the assert mechanism is only for development, testing, and debugging. Turning off assertions in production code is required to still result in correct behavior.
 
Rajdeep Biswas
Ranch Hand
Posts: 224
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic