throw IllegalArgumentException from Enum.valueOf if name does not match any value

This commit is contained in:
Joel Dice 2009-08-13 08:57:58 -06:00
parent fb37f48237
commit 864a28f2ce

View File

@ -26,7 +26,8 @@ public abstract class Enum<E extends Enum<E>> implements Comparable<E> {
} }
public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) { public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) {
if (name != null) { if (name == null) throw new NullPointerException();
try { try {
Method method = enumType.getMethod("values"); Method method = enumType.getMethod("values");
Enum values[] = (Enum[]) (method.invoke(null)); Enum values[] = (Enum[]) (method.invoke(null));
@ -38,8 +39,8 @@ public abstract class Enum<E extends Enum<E>> implements Comparable<E> {
} catch (Exception ex) { } catch (Exception ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
}
return null; throw new IllegalArgumentException(name);
} }
public int ordinal() { public int ordinal() {