Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertions

 
sweety singh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi...
1. i wanted to know if both the following declaration for enabling and disabling the assertions at runtime are correct.
java -ea com.geelcsanonymous.Foo and
java -ea:com.geeksanonymous.Foo

2. can anyone give me an example for not using assertions to validate command line arguments.

3. i have not understood this:

code:

switch(x) {
case 2: y = 3;
case 3: y = 9;
case 4: y = 27;
default: assert false; // We're never supposed to get here!
}
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I shall only answer one of your questions, the third. You have fall-through in that "switch" so you will reach the default.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sweety singh:
2. can anyone give me an example for not using assertions to validate command line arguments.


Dunno what you mean by an example. I can give reasons, if that's what you mean.

Assertions are for checking internal assumptions and consistency of a program. The conditions checked by assertions should be ones that are always true, unless there is a bug in the program.

Assertions are not for checking user input, resource availability, I/O success etc. Remember that assertions are turned off in release code, so if they were used for such checks, the checks would not happen in release code.

Command-line arguments are user input. Bad command-line arguments are not a bug in the program that's reading them. Therefore assertions should not be used for checking them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic