• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about assertions...

 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From KnB book:

It is sometimes good practice to throw an AssertionError explicitly.

hmm.. I don't quite get that one.. can any one elaborate it please?
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about when your program should not reach that a particular point in your code... so you directly call
assert false;
You know for sure that it must throw an AssertionError because it reached that point...
am I right?
 
G Nadeem
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
some time we have to throw explicitly. as in following case.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks "G"..
but is it valid to say only:
assert false; ?
or you need to throw AssertionError?
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andres
It is sometimes advisable to throw AssertionError explictily becasue the assertions can be disabled at runtime.
Answer to you question "why not simply use assert false;" is also the same i.e. assert false; may be disabled at runtime. So in case you want that this point in the program should never ever be reached then you can throw an assertion error.
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anupam -
couldn't have said it better myself...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic