Check if enum exists in Java
Is there anyway to check if an enum exists by comparing it to a given string? I can't seem to find any such function. I could just try to use the valueOf method and catch an exception but I'v been taught that catching runtime exceptions is not good practice. Anybody have any ideas? I don't think there's a built-in way to do it without catching exceptions. You could instead use something like this: public static MyEnum asMyEnum(String str) { for (MyEnum me : MyEnum.values()) { if (me.name().equalsIgnoreCase(str)) return me; } return null; } Edit: As Jon Skeet notes, values() works by cloning a