title: Retrieving an enum using 'valueOf' throws RuntimeException - What to use instead?
body: <p>I have the following enum</p>
  
  <pre><code>enum Animal implements Mammal {
     CAT, DOG;
  
     public static Mammal findMammal(final String type) {
        for (Animal a : Animal.values()) {
           if (a.name().equals(type)) {
              return a;
           }
        }
     }
  }
  </code></pre>
  
  <p>I had originally used the <code>Enum.valueOf(Animal.class, "DOG");</code> to find a particular Animal. However, I was unaware that if a match is not found, an <code>IllegalArgumentException</code> is thrown. I thought that maybe a null was returned. So this gives me a problem. I don't want to catch this <code>IllegalArgumentException</code> if a match is not found. I want to be able to search all enums of type <code>Mammal</code> and I don't want to have to implement this static '<code>findMammal</code>' for each enum of type <code>Mammal</code>. So my question is, what would be the most auspicious design decision to implement this behaviour? I will have calling code like this:</p>
  
  <pre><code>public class Foo {
     public Mammal bar(final String arg) {
        Mammal m = null;
        if (arg.equals("SomeValue")) {
           m = Animal.findMammal("CAT");
        } else if (arg.equals("AnotherValue") {
           m = Human.findMammal("BILL");
        }
        // ... etc
     }
  }
  </code></pre>
  
  <p>As you can see, I have different types of Mammal - 'Animal', 'Human', which are enums. I don't want to have to implement 'findMammal' for each Mammal enum. I suppose the best bet is just create a utility class which takes a Mammal argument and searches that? Maybe there's a neater solution. </p>