8 Replies Latest reply on Nov 17, 2006 3:27 PM by 807599

    IllegalArgumentException => hard to catch it.. (help)

    807599
      Hi there,
      I've got little problem catching the "IllegalArgumentException", if you guys know how to solve this problem that'd help me a lot..

      The code works, but if I type something else than what's assigned in Enum it shows IllegalArgumentException...

      TIA

             public Options paper, rock, scissors;
         
           //assign variables
             paper = Options.paper;
             rock = Options.rock;
             scissors = Options.scissors;
      
           String Playing;
      
      
      //here comes the code.... = problem................................
      //Select one of these only!
      while(     !(Options.valueOf(thePlay) == Options.rock) && !(Options.valueOf(thePlay) == Options.paper) && !(Options.valueOf(thePlay) == Options.scissors)  )
      {
      
               System.out.println("\n It's your turn select ONE:  rock,  paper,  scissors "); 
      
           try{
      
      }
            catch (IllegalArgumentException e)
           {
                Playing = scan.next().toLowerCase(); 
            }
      
      }
      .......
      .......
      Error:
      Exception in thread "main" java.lang.IllegalArgumentException: No enum const class...
        • 1. Re: IllegalArgumentException => hard to catch it.. (help)
          807599
          Where you catch an exception, print the stack trace:
          } catch (AnyException e) {
            e.printStackTrace();
            // plus anything else you want
          }
          Don't throw information away.

          It would help if you posted the actual error and stack trace you're getting.
          • 2. Re: IllegalArgumentException => hard to catch it.. (help)
            807599
            Thanks for the reply paulcw ,

            I've changed the code into following.. but still getting the error..


            Exception in thread "main" java.lang.IllegalArgumentException: No enum const cla
            ss RockPaperScissors$Game.dffg
            at java.lang.Enum.valueOf(Enum.java:192)
            at RockPaperScissors$Game.valueOf(RockPaperScissors.java:33)
            at RockPaperScissors.main(RockPaperScissors.java:181)

                 try{
            
                      Playing = scan.next().toLowerCase(); 
             
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                 Playing = scan.next().toLowerCase(); 
            }
            Message was edited by:
            SunKid
            • 3. Re: IllegalArgumentException => hard to catch it.. (help)
              807599
              OK and what's on line 33?
              • 4. Re: IllegalArgumentException => hard to catch it.. (help)
                807599
                Line 33     public enum Game{rock, paper, scissors}
                Line 181 if (Game.valueOf(Playing) == Options.paper)
                • 5. Re: IllegalArgumentException => hard to catch it.. (help)
                  807599
                  OK, now rereading your post I'm kind of confused what the problem is. You're trying to look something up that deliberately doesn't exist, and you get an exception. That's normal behavior.

                  Your try/catch block isn't structured right....you seem to be trying to catch stuff that happens outside the try block, which makes no sense. But it's poor design to rely on try/catch for normal execution anyway.

                  What are you trying to accomplish?
                  • 6. Re: IllegalArgumentException => hard to catch it.. (help)
                    807599
                    Hi paulcw,
                    the code maybe messy, but I can try to explain you what I'm trying to do here..

                    you see,, I've got 3 options in enum, paper, rock and scissors, so after a succsssful compilation when I type them it's works just fine, but the problem is, when it ask to choose one from these 3 options... if I type some other string or integers It shows following exceptions..

                    I want to make sure that whenever user type something else than those 3 options it should ask again to re-type one of the option INSTEAD showing the exception.

                    I hope u can understand my problem better now..


                    Exception in thread "main" java.lang.IllegalArgumentException: No enum const class...
                    • 7. Re: IllegalArgumentException => hard to catch it.. (help)
                      807599
                      I see.

                      A few ways occur to me. Given this:
                      enum Stuff {dog, cat, monkey}
                      You could do this:
                      Stuff s = null;
                      
                      do {
                        try {
                          s = Stuff.valueOf(Input.getString());
                        } catch (IllegalArgumentException e) {
                          System.err.println("that's not a valid Stuff: " + e);
                        }
                      } while(s == null);
                      
                      System.out.println("s is: " + s);
                      which works, but is ugly.

                      Or you could do this:
                      Stuff s = null;
                      
                      ASKUSER: while(true) {
                        String in = Input.getString();
                        for(Stuff t : Stuff.values()) {
                          if (t.name().equals(in)) {
                            s = t;
                            break ASKUSER;
                          }
                        }
                        System.err.println("That's not a valid value: " + in);
                      }
                      
                      System.out.println("s is: " + s);
                      Or you could do this:
                      Map<String, Stuff> vals = new HashMap<String, Stuff>();
                      for(Stuff t : Stuff.values())
                        vals.put(t.name(), t);
                      
                      // then later:
                      
                      Stuff s = null;
                      
                      do {
                        s = vals.get(Input.getString());
                        if (s == null)
                          System.err.println("That's not a valid value");
                      } while(s == null);
                      
                      System.out.println("s is: " + s);
                      which might be worthwhile if you're doing a lot of lookups and there are a lot of values in the enum.

                      Another way which may be best is the following. First, define your enum like this:
                      enum Stuff {
                        dog, cat, monkey;
                      
                        public static Stuff niceValueOf(String s) {
                          for(Stuff t : values())
                            if (t.name().equals(s))
                              return t;
                          return null;
                        } 
                      }
                      Then you could just do this;
                      Stuff s = null;
                      
                      do {
                        s = Stuff.niceValueOf(Input.getString());
                        if (s == null)
                          System.err.println("That's not a valid value");
                      } while(s == null);
                      
                      System.out.println("s is: " + s);
                      I haven't used enums a whole lot myself, so maybe there's a better approach I'm not familiar with.
                      • 8. Re: IllegalArgumentException => hard to catch it.. (help)
                        807599
                        I suppose one philosophical question behind this is, is it fair for an enum variable to ever have the value null? As a comparison, primitives can never be null.