wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about assertions..." Watch "Question about assertions..." New topic
Author

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
hi,
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: 1088
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

Joined: Oct 14, 2002
Posts: 8801
    
    5
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: http://aspose.com/file-tools
 
subject: Question about assertions...
 
Similar Threads
The Olympic Spirit?!?
Gold man sach technical interview
How many Object is created?
PreparationKit_Simulator
Deaf Java programmers around?