What results when you pass an invalid String to a Java enum .valueOf call?
method: valueOf
Returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
Parameters:
enumType - the Class object of the enum type from which to return a constant
name - the name of the constant to return
Returns:
the enum constant of the specified enum type with the specified name
Throws:
IllegalArgumentException - if the specified enum type has no constant with the specified name, or **the specified class object does not represent an enum type**
NullPointerException - if **enumType or name is null**
so it will flag these exceptions,
I just tried your code. It throws an IllegalArgumentException
. Just like the documentation says.
Status.valueOf behaves the same as Enum.valueOf
You should get an IllegalArgumentException
if the name is not that of an enum (which it wont be for the empty string). This is generated in the API docs for all enum valueOf
methods. You should get a NullPointerException
for null
. It's probably not a good idea to give a dummy value to your String
variable (nor to allow the last case
/default
to fall through).