By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,638 Members | 1,364 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,638 IT Pros & Developers. It's quick & easy.

Wrong exception being thrown

P: 9
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 IllegalArgumentException 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.

Expand|Select|Wrap|Line Numbers
  1.       public void setMonthName(String inSetString)
  2.       {
  3.          int i = 0;
  4.          monthNumber = -1;
  5.  
  6.          try
  7.          {
  8.             i = Integer.parseInt(inSetString);
  9.             if (i >= 1 && i <= 12)
  10.             {
  11.                     monthNumber = i;
  12.             }
  13.          }
  14.             catch (IllegalArgumentException e)
  15.             {        
  16.  
  17.             }
  18.  
  19.       // if it gets to here we know it's a string
  20.          for (int index = 0; index < monthNames.length; index++)
  21.          {
  22.             if (monthNames[index].equalsIgnoreCase(inSetString))
  23.             {
  24.                monthNumber = index;
  25.             }
  26.          }
  27.          if (monthNumber == -1)
  28.          {
  29.             throw new IllegalArgumentException("Invalid month name string: " + inSetString);
  30.          }
  31.       }
Feb 21 '12 #1

✓ answered by r035198x

So structure your code like this:
Expand|Select|Wrap|Line Numbers
  1.  try
  2.     {
  3.       System.out.println("first: " + inSetString);
  4.       i = Integer.parseInt(inSetString);
  5.       System.out.println("next: " + inSetString);
  6.       if ( i >= 1 && i <= 12 )
  7.       {
  8.         monthNumber = i;
  9.       }
  10.     }
  11.     catch (NumberFormatException e)
  12.     {
  13.       // if it gets to here we know it's a string
  14.       for (int index = 0; index < monthNames.length; index++)
  15.       {
  16.         if ( monthNames[index].equalsIgnoreCase(inSetString) )
  17.         {
  18.           monthNumber = index;
  19.         }
  20.       }
  21.       if ( monthNumber == -1 )
  22.       {
  23.         throw new NumberFormatException("Invalid month name string: " + inSetString);
  24.       }
  25.     }
  26.     if ( monthNumber == -1 )
  27.     {
  28.       throw new NumberFormatException("Illegal month Number: " + inSetString);
  29.     }
  30.  

Share this Question
Share on Google+
12 Replies


10K+
P: 13,264
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.

Expand|Select|Wrap|Line Numbers
  1. try
  2.     {
  3.       System.out.println("first: " + inSetString);
  4.       i = Integer.parseInt(inSetString);
  5.       System.out.println("next: " + inSetString);
  6.       if ( i >= 1 && i <= 12 )
  7.       {
  8.         monthNumber = i;
  9.       }
  10.     }
  11.     catch (NumberFormatException e)
  12.     {
  13.       // if it gets to here we know it's a string
  14.       for (int index = 0; index < monthNames.length; index++)
  15.       {
  16.         if ( monthNames[index].equalsIgnoreCase(inSetString) )
  17.         {
  18.           monthNumber = index;
  19.         }
  20.       }
  21.     }
  22.     if ( monthNumber == -1 )
  23.     {
  24.       throw new IllegalArgumentException("Invalid month name string: " + inSetString);
  25.     }
  26.  
  27.  
Also consider the case when someone passes in a number that is not between 1 and 12. What happens then?
Feb 21 '12 #2

P: 9
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 IllegalArgumentException instead of the 1st one and giving the wrong System.out.print() message.

If I add a throw new IllegalArgumentException 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 IllegalArgumentException instead of the 2nd one, and giving the wrong System.out.print() message.

So either way, it's not working for both types of input.
Feb 21 '12 #3

10K+
P: 13,264
The code you posted has an empty catch block. Compare that with the code I posted.
Feb 21 '12 #4

P: 9
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 IllegalArgumentException and goes to the 2nd one and gives the wrong System.out.print()

Expand|Select|Wrap|Line Numbers
  1.       public void setMonthName(String inSetString)
  2.       {
  3.          int i = 0;
  4.          monthNumber = -1;
  5.  
  6.          try
  7.          {
  8.             i = Integer.parseInt(inSetString);
  9.             if (i < 1 || i > 12)
  10.             {
  11.                throw new IllegalArgumentException("Illegal month number here2: " + inSetString);
  12.             }
  13.             else
  14.             {
  15.                monthNumber = i;
  16.             }
  17.  
  18.          }
  19.             catch (IllegalArgumentException e)
  20.             {        
  21.             // if it gets to here we know it's a string
  22.                for (int index = 0; index < monthNames.length; index++)
  23.                {
  24.                   if (monthNames[index].equalsIgnoreCase(inSetString))
  25.                   {
  26.                      monthNumber = index + 1;
  27.                   }
  28.                }
  29.                if (monthNumber == -1)
  30.                {
  31.                   throw new IllegalArgumentException("Invalid month name string: " + inSetString);
  32.                }
  33.             }
  34.       }
Feb 21 '12 #5

10K+
P: 13,264
Did you read my first reply at all?
Feb 21 '12 #6

P: 9
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.
Feb 21 '12 #7

10K+
P: 13,264
Don't do
Expand|Select|Wrap|Line Numbers
  1. if (i < 1 || i > 12)
  2.    {
  3.        throw new IllegalArgumentException("Illegal month number here2: " + inSetString);
  4.    }
  5.  
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
Expand|Select|Wrap|Line Numbers
  1.  if ( monthNumber == -1 )
  2.     {
  3.       throw new IllegalArgumentException("Invalid month name string: " + inSetString);
  4.     }
  5.  
is after the catch block?
Feb 21 '12 #8

P: 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.
Feb 21 '12 #9

10K+
P: 13,264
So structure your code like this:
Expand|Select|Wrap|Line Numbers
  1.  try
  2.     {
  3.       System.out.println("first: " + inSetString);
  4.       i = Integer.parseInt(inSetString);
  5.       System.out.println("next: " + inSetString);
  6.       if ( i >= 1 && i <= 12 )
  7.       {
  8.         monthNumber = i;
  9.       }
  10.     }
  11.     catch (NumberFormatException e)
  12.     {
  13.       // if it gets to here we know it's a string
  14.       for (int index = 0; index < monthNames.length; index++)
  15.       {
  16.         if ( monthNames[index].equalsIgnoreCase(inSetString) )
  17.         {
  18.           monthNumber = index;
  19.         }
  20.       }
  21.       if ( monthNumber == -1 )
  22.       {
  23.         throw new NumberFormatException("Invalid month name string: " + inSetString);
  24.       }
  25.     }
  26.     if ( monthNumber == -1 )
  27.     {
  28.       throw new NumberFormatException("Illegal month Number: " + inSetString);
  29.     }
  30.  
Feb 21 '12 #10

P: 9
That looks like it would use the correct System.out.print() messages. But the problem is that I've been required to use IllegalArgumentException.
Feb 21 '12 #11

10K+
P: 13,264
Just change the NumberFormatException to IllegalArgumentException.
Feb 21 '12 #12

P: 9
Ok, that works. I see what the problem was now. Thanks a lot.
Feb 21 '12 #13

Post your reply

Sign in to post your reply or Sign up for a free account.