title: Safe use of Enum valueOf for String comparison on a switch
body: <p>How to safe implement the use of valueOf in case i get a String different than the supported on the enum <code>ACTION</code>. I mean is possible to force to <code>ACTION.valueOf(valueToCompare)</code> to get a valid value even when happens that <code>valueToCompare</code> is not a valid enum member</p>
  
  <p>I get an expected execution when <code>valueToCompare</code> is <code>"CRY"</code> or <code>"CRYALOT"</code> or <code>"cryalot"</code> etc. </p>
  
  <p>And i get <code>java.lang.IllegalArgumentException</code> on cases like in the code.</p>
  
  <pre><code>public enum ACTION{
      CRY,
      CRYALOT;
  }
  
  public static void main(String[] args) {
  
      String valueTocompare = "posible not expected value".toUpperCase();
  
      switch (ACTION.valueOf(valueToCompare)) {
          case CRY:
              System.out.println("Cry");
              break;
          case CRYALOT:
              System.out.println("Cry a lot");
              break;
          default:
              System.out.println("catch posible not expected value");
              break;
      }
  }
  </code></pre>
  
  <p>EDIT &amp; used SOLUTION:</p>
  
  <p>I solved this by using a try-catch as @Peter Lawrey suggested:</p>
  
  <pre><code>public enum ACTION{
      CRY,
      CRYALOT,
      NOTVALID;
  }
  
  public static void main(String[] args) {
  
      String valueToCompare = "NOTVALID";
      ACTION action;
      try {
          valueToCompare= "variable posible not expected value".toUpperCase();
          action = ACTION.valueOf(valueToCompare);
      } catch(IllegalArgumentException e){
          System.out.println("Handled glitch on the Matrix");
          action = ACTION.NOTVALID;
      }
  
      switch (action) {
          case CRY:
              System.out.println("Cry");
              break;
          case CRYALOT:
              System.out.println("Cry a lot");
              break;
          default:
              System.out.println("catch posible not expected value");
              break;
      }
  
      System.out.println("We continue normal execution on main thread...");
  }
  </code></pre>