my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B Chapter 4: Assertions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B Chapter 4: Assertions" Watch "K&B Chapter 4: Assertions" New topic
Author

K&B Chapter 4: Assertions

Marco Beelen
Greenhorn

Joined: Feb 23, 2005
Posts: 4

In the section about Using Assertions Appropriately, it states the 'assert false'-statement can be used if a section of code never should be reached.
It is shown with an example of a switch-case-statement:

switch (x) {
case 2: y=3;
case 3: y=17;
case 4: y=27;
default: assert false; // We're never supposed to get here.
}

The example isn't very good IMHO, because this example would qualify to be an exam question, because based upon the fall-through this would always throw an AssertionError, due to this missing break; statements.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
That is the point. The assertion statement quickly flags a logic error that otherwise would be harder to find.


Mike Gershman
SCJP 1.4, SCWCD in process
Adam Czysciak
Ranch Hand

Joined: Feb 25, 2005
Posts: 90
    
    5
Originally posted by Marco Beelen:



The example isn't very good IMHO, because this example would qualify to be an exam question, because based upon the fall-through this would always throw an AssertionError, due to this missing break; statements.


No, this will throw AssertionError only if assertions are enabled.


Adam
SCJA, SCJP 5.0, SCWCD 1.4, SCBCD 5, SCEA 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B Chapter 4: Assertions