473,480 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Wrong exception being thrown

9 New Member
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
12 1650
r035198x
13,262 MVP
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
Jerick
9 New Member
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
r035198x
13,262 MVP
The code you posted has an empty catch block. Compare that with the code I posted.
Feb 21 '12 #4
Jerick
9 New Member
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
r035198x
13,262 MVP
Did you read my first reply at all?
Feb 21 '12 #6
Jerick
9 New Member
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
r035198x
13,262 MVP
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
Jerick
9 New Member
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
r035198x
13,262 MVP
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
Jerick
9 New Member
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
r035198x
13,262 MVP
Just change the NumberFormatException to IllegalArgumentException.
Feb 21 '12 #12
Jerick
9 New Member
Ok, that works. I see what the problem was now. Thanks a lot.
Feb 21 '12 #13

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

Similar topics

5
4750
by: Fao, Sean | last post by:
I'm reading The C++ Programming Language by Bjarne Stroustrup and I was unclear as to the proper way to catch an exception thrown by the new keyword. I was wondering if someobody here could let me...
2
5940
by: Tyler Foreman | last post by:
Hello, I had an earlier post regarding a strange exception I (thought) I was getting in my windows form. I have since been able to trace the problem to a picturebox control on my form. I can...
1
1188
by: Mark | last post by:
We have an internal web service that intentionally allows raw Exceptions to be thrown to the clients that consume it. This web service is consumed by internal ASP.NET applications. When the...
1
13931
by: R | last post by:
Hi All, I'm using PHP 5, my code fully separates code from content, my code throws exceptions (LIZException) when error occurs. but every time I throw exception I get this fatal error: ...
1
2784
by: yancheng.cheok | last post by:
Hi all, According to "How can I handle a constructor that fails?" in http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.2, whenever there is a constructor fail, we will throw...
1
3058
by: matt.arens | last post by:
here is some background: I am using the MQSeries/Websphere .Net classes provided by Microsoft. The Websphere Windows Client software is required in order to use the ..Net classes. However, I...
0
1067
by: drop | last post by:
Hi, I have some DropDownLists that are contained in a DetailsView. Here's a sample of my code for the DropDownLists : <InsertItemTemplate> <asp:DropDownList ID="ddl_TourOp" runat="server"...
0
1086
by: sethuraman | last post by:
Hi, I have had a problem before, I am not sure about the exact exception that is thrown.. but I am sure that there is an exception thrown after processing 600 and odd rows in a DB2 JDBC...
1
2346
by: sudhivns | last post by:
Function F1() throwing an exception of type say int. But this exception is not getting caught. piece of code is: try { .... F1() ..... } catch(int x) {
1
2136
by: pasu | last post by:
I'm getting the error Exception thrown and not caught...and showing the line number too.but nothing i've wrote on that line......greatly confusing...kindly help me....
0
7041
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7084
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6739
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6929
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4779
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
181
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.