Wrong exception being thrown

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jerick
    New Member
    • Feb 2012
    • 9

    Wrong exception being thrown

    I'm trying to account for any value outside of 1 to 12 or a name of a month. I'm passing in a string to this method. I then try to parse it into an int and do an if statement to see if it's between 1 and 12. If it's not an int then it goes down and a for-loop checks to see if it's a name of a month.

    The problem is that if I add a throw new IllegalArgument Exception to the catch block and pass in "Saturday" it throws the Illegal month number here message instead of Invalid month name string message. If I leave the catch block empty, then numbers outside the range of 1 to 12 go down and throw the Invalid month name string message.

    Code:
          public void setMonthName(String inSetString)
          {
             int i = 0;
             monthNumber = -1;
          	
             try
             {
                i = Integer.parseInt(inSetString);
                if (i >= 1 && i <= 12)
                {
    					monthNumber = i;
                }
             }
                catch (IllegalArgumentException e)
                {		
    
                }
          		
          // if it gets to here we know it's a string
             for (int index = 0; index < monthNames.length; index++)
             {
                if (monthNames[index].equalsIgnoreCase(inSetString))
                {
                   monthNumber = index;
                }
             }
             if (monthNumber == -1)
             {
                throw new IllegalArgumentException("Invalid month name string: " + inSetString);
             }
          }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    We know it's not a number if we get inside the catch not after the catch so your string logic must go inside the catch block.

    [code=java]
    try
    {
    System.out.prin tln("first: " + inSetString);
    i = Integer.parseIn t(inSetString);
    System.out.prin tln("next: " + inSetString);
    if ( i >= 1 && i <= 12 )
    {
    monthNumber = i;
    }
    }
    catch (NumberFormatEx ception e)
    {
    // if it gets to here we know it's a string
    for (int index = 0; index < monthNames.leng th; index++)
    {
    if ( monthNames[index].equalsIgnoreCa se(inSetString) )
    {
    monthNumber = index;
    }
    }
    }
    if ( monthNumber == -1 )
    {
    throw new IllegalArgument Exception("Inva lid month name string: " + inSetString);
    }

    [/code]

    Also consider the case when someone passes in a number that is not between 1 and 12. What happens then?

    Comment

    • Jerick
      New Member
      • Feb 2012
      • 9

      #3
      With the code I posted, strings such as "January" or "Saturday" were being hanlded correctly, but numbers outside of 1 to 12 were being sent down to the 2nd IllegalArgument Exception instead of the 1st one and giving the wrong System.out.prin t() message.

      If I add a throw new IllegalArgument Exception to the catch block, then numbers outside of 1 to 12 get handled correctly, but strings such as "January" or "Saturday" are being given the 1st IllegalArgument Exception instead of the 2nd one, and giving the wrong System.out.prin t() message.

      So either way, it's not working for both types of input.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        The code you posted has an empty catch block. Compare that with the code I posted.

        Comment

        • Jerick
          New Member
          • Feb 2012
          • 9

          #5
          In the code I posted, a string like "December" just skips over the empty catch block and goes to the for-loop.

          But here's some code where I entered the for-loop into the catch block. Strings like "December" are working, but if I enter in a number outside of 1 to 12, it passes the 1st IllegalArgument Exception and goes to the 2nd one and gives the wrong System.out.prin t()

          Code:
                public void setMonthName(String inSetString)
                {
                   int i = 0;
                   monthNumber = -1;
                	
                   try
                   {
                      i = Integer.parseInt(inSetString);
                      if (i < 1 || i > 12)
                      {
                         throw new IllegalArgumentException("Illegal month number here2: " + inSetString);
                      }
                      else
                      {
                         monthNumber = i;
                      }
                   	
                   }
                      catch (IllegalArgumentException e)
                      {		
                      // if it gets to here we know it's a string
                         for (int index = 0; index < monthNames.length; index++)
                         {
                            if (monthNames[index].equalsIgnoreCase(inSetString))
                            {
                               monthNumber = index + 1;
                            }
                         }
                         if (monthNumber == -1)
                         {
                            throw new IllegalArgumentException("Invalid month name string: " + inSetString);
                         }
                      }
                }

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Did you read my first reply at all?

            Comment

            • Jerick
              New Member
              • Feb 2012
              • 9

              #7
              Yup... I added an if else statement to the code I just posted which is why numbers between 1 to 12 aren't working anymore. Simply moving the for-loop into the catch doesn't change anything.

              Did YOU read my first post? I explained all of this. Read the 2nd paragraph in my first post, please.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Don't do
                [code=java]
                if (i < 1 || i > 12)
                {
                throw new IllegalArgument Exception("Ille gal month number here2: " + inSetString);
                }
                [/code]
                if (i < 1 || i > 12) then monthNumber is never assigned a value which means that it's value is -1 after the catch block.

                Do you see that in my code
                [code=java]
                if ( monthNumber == -1 )
                {
                throw new IllegalArgument Exception("Inva lid month name string: " + inSetString);
                }
                [/code] is after the catch block?

                Comment

                • Jerick
                  New Member
                  • Feb 2012
                  • 9

                  #9
                  I put the -1 there so that if the value doesn't get changed, then that means it didn't get parsed into an int, which means it's a string. If it doesn't go through the for-loop, then the value still isn't changed and it throws the exception.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    So structure your code like this:
                    [code=java]
                    try
                    {
                    System.out.prin tln("first: " + inSetString);
                    i = Integer.parseIn t(inSetString);
                    System.out.prin tln("next: " + inSetString);
                    if ( i >= 1 && i <= 12 )
                    {
                    monthNumber = i;
                    }
                    }
                    catch (NumberFormatEx ception e)
                    {
                    // if it gets to here we know it's a string
                    for (int index = 0; index < monthNames.leng th; index++)
                    {
                    if ( monthNames[index].equalsIgnoreCa se(inSetString) )
                    {
                    monthNumber = index;
                    }
                    }
                    if ( monthNumber == -1 )
                    {
                    throw new NumberFormatExc eption("Invalid month name string: " + inSetString);
                    }
                    }
                    if ( monthNumber == -1 )
                    {
                    throw new NumberFormatExc eption("Illegal month Number: " + inSetString);
                    }
                    [/code]

                    Comment

                    • Jerick
                      New Member
                      • Feb 2012
                      • 9

                      #11
                      That looks like it would use the correct System.out.prin t() messages. But the problem is that I've been required to use IllegalArgument Exception.

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Just change the NumberFormatExc eption to IllegalArgument Exception.

                        Comment

                        • Jerick
                          New Member
                          • Feb 2012
                          • 9

                          #13
                          Ok, that works. I see what the problem was now. Thanks a lot.

                          Comment

                          Working...