What results when you pass an empty String (or some other unrecognized value, or a null) to a Java enum .valueOf call?
For example:
public enum Status
{
STARTED,
PROGRESS,
MESSAGE,
DONE;
}
and then
String empty = "";
switch(Status.valueOf(empty))
{
case STARTED:
case PROGRESS:
case MESSAGE:
case DONE:
{
System.out.println("is valid status");
break;
}
default:
{
System.out.println("is not valid");
}
}
Basically, I want to know if I’m using a switch statement with the enum, will the default case be called or will I get an exception of some sort?
You should get an
IllegalArgumentExceptionif the name is not that of an enum (which it won’t be for the empty string). This is generated in the API docs for all enumvalueOfmethods. You should get aNullPointerExceptionfornull. It’s probably not a good idea to give a dummy value to yourStringvariable (nor to allow the lastcase/defaultto fall through).