Tutorial :Enums With Default Throw Clause?


I noticed the following in the Java Language spec in the section on enumerations here: link

switch(this) {    case PLUS:   return x + y;    case MINUS:  return x - y;    case TIMES:  return x * y;    case DIVIDE: return x / y;  }  throw new AssertionError("Unknown op: " + this);  

However, looking at the switch statement definition section, I didn't notice this particular syntax (the associated throw statement) anywhere.

Can I use this sort of "default case is throw an exception" syntactic sugar outside of enum definitions? Does it have any special name? Is this considered a good/bad practice for short-cutting this behavior of "anything not in the list throws an exception"?


I am not sure if I get you, but you seem to believe that the throw is part of the switch syntax in the posted code sample. That is not the case. The switch block and the throw statement are two seperate things, which just happens to be placed next to each other in this code.

In more detail: The four case parts in the switch all contain return statements, causing any subsequent instructions in the method to be skipped. If none of the case parts matches, execution continues on the line following the switch block, which happens to be a throw.

You could use a throw after an if in a very similar way:

if (something) {      return aValue;  }    throw new Exception("Nope");  


Shame on the writer of that tutorial for throwing an AssertionError - why not make use of the Exceptions that Java provides us already, like UnsupportedOperationException


There's a default keyword:

switch (whatever) {    // ...    default: explode();  }  

If you want the default case to throw an exception, then

  default: throw new Explode();  

Your example only works the way it does because all the other branches return from the containing function. In my opinion that's not a very good coding habit to get into.


In eclipse, if you do not cover all enum values in a switch statement it generates a warning at compile time.

If, however, you put a default: case that warning does not work anymore. So, yes in some IDEs there is a definite advantage of omitting default case when all enum values are covered in a switch statement.

In general, if your switch statement grows beyond 5-6 cases, consider redesigning the code. Usually, people do not appreciate that enums in Java are full blown classes and enum values are first-class objects, so instead of driving the behavior through the switch statement in many cases you can ask the enum object to do the work itself.

In this particular example the enum can have a method doOp( int x, int y) and each enum member can implement a particular flavor of this method.


As others have stated, this is not syntax sugar. It works because all of the branches on the switch return from the method. If some other enum value was put into the switch, the switch statement would complete without running any of the branches, after which the throw statement would execute.

switch(this) {    case PLUS:   return x + y;    case MINUS:  return x - y;    case TIMES:  return x * y;    case DIVIDE: return x / y;    default: throw new AssertionError("Unknown op: " + this);  }  

That code would be functionally equivalent. The major difference between the two is that your switch with no default clause will create a compiler warning if a value is added to the Enum but not to the switch statement.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »