-3

My book on Java seems to suggest that in switch statements each case that isn't the default should cover an exceptional condition. What is the meaning of this and why is it recommended? Can you provide any examples to illustrate the point?

Edit: I think I got the meaning/implication of the original quote: Inputs or situations that don't meet your initial expectations almost always need to be covered, and the default case in switch statements supplies a way to do so.

progner
  • 139
  • 5
  • Maybe what the author meant was that if there's only one condition per switch-case, you should use an if-else structure, but I can't be sure. – Luan Nico Jan 19 '17 at 14:17
  • 1
    No way to answer without context. – Dave Newton Jan 19 '17 at 14:17
  • Added an image with the quote. – progner Jan 19 '17 at 14:18
  • 3
    This give an explanation of what the author meant by that http://stackoverflow.com/a/5241196/1925997 – Kishore Bandi Jan 19 '17 at 14:19
  • 3
    That's not even what it says. It says to provide a default handler so you are forced to handle conditions that don't match any of the case statements--*that* is the "exceptional condition"--conditions that don't match the cases you provide. – Dave Newton Jan 19 '17 at 14:19
  • Possible duplicate of [Should switch statements always contain a default clause?](http://stackoverflow.com/questions/4649423/should-switch-statements-always-contain-a-default-clause) – SomeJavaGuy Jan 19 '17 at 14:19
  • @DaveNewton I didn't say it said so. It was suggested or implied. – progner Jan 19 '17 at 14:21
  • No it isn't-what you said is basically backwards. The cases you provide are explicitly *not* exceptional, because you expected them, and coded for them. "Exceptional" means rare or unanticipated. – Dave Newton Jan 19 '17 at 14:22
  • It is somewhat counter-intuitive that the _default_ case handles _exceptional_ conditions, since "default" and "exceptional" are essentially opposites. – khelwood Jan 19 '17 at 14:31
  • Thanks for the helpful edit to your second comment, @DaveNewton. – progner Jan 19 '17 at 14:31
  • Might indeed have been what tripped me up, @khelwood – progner Jan 19 '17 at 14:33

1 Answers1

1

I think you misunderstood what the author wrote. He said:

Including a default case focuses you on the need to process exceptional conditions.

What he meant is, you are going to add everything you are thing to the switch case, and then adding a default will guarantee that you will think about every other possibility. For instance, if you switched for animals, "cat", "dog", "pig", well, what if the user input something else? Adding a default forces you to think about that, and handle errors and exceptional cases appropriately.

Luan Nico
  • 5,376
  • 2
  • 30
  • 60
  • Perhaps I did misunderstand it after all. My original thoughts were that it meant exceptional conditions should be covered in all cases, and anything regular in the default. – progner Jan 19 '17 at 14:26
  • No, that's pretty much the opposite: handle regular cases, and add a default case to force yourself to think about all other (exceptional) cases -- those are very easy to forget, when you have something in your head, like "it's going to be 'Y' or 'N'", handling other cases you don't want to see scarcely crosses the mind. – Luan Nico Jan 19 '17 at 14:31
  • 1
    BTW: not having a deault when you switch on an enum can be helpfull, as it allows you to recognize if a new enum was added but no handling code for it. – eckes Jan 19 '17 at 14:48