• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B Chapter 4: Assertions

 
Marco Beelen
Greenhorn
Posts: 4
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the point. The assertion statement quickly flags a logic error that otherwise would be harder to find.
 
Adam Czysciak
Ranch Hand
Posts: 90
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic