File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about assertions... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about assertions..." Watch "Question about assertions..." New topic

Question about assertions...

Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
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?

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
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

Joined: Apr 25, 2003
Posts: 48
some time we have to throw explicitly. as in following case.
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Thanks "G"..
but is it valid to say only:
assert false; ?
or you need to throw AssertionError?
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
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

Joined: Oct 14, 2002
Posts: 8898
Anupam -
couldn't have said it better myself...

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
I agree. Here's the link:
subject: Question about assertions...
It's not a secret anymore!