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. - 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);
-
}
-
}
12 1650
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. -
try
-
{
-
System.out.println("first: " + inSetString);
-
i = Integer.parseInt(inSetString);
-
System.out.println("next: " + inSetString);
-
if ( i >= 1 && i <= 12 )
-
{
-
monthNumber = i;
-
}
-
}
-
catch (NumberFormatException 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);
-
}
-
-
Also consider the case when someone passes in a number that is not between 1 and 12. What happens then?
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.
The code you posted has an empty catch block. Compare that with the code I posted.
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() - 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);
-
}
-
}
-
}
Did you read my first reply at all?
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.
Don't do -
if (i < 1 || i > 12)
-
{
-
throw new IllegalArgumentException("Illegal month number here2: " + inSetString);
-
}
-
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 -
if ( monthNumber == -1 )
-
{
-
throw new IllegalArgumentException("Invalid month name string: " + inSetString);
-
}
-
is after the catch block?
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.
So structure your code like this: -
try
-
{
-
System.out.println("first: " + inSetString);
-
i = Integer.parseInt(inSetString);
-
System.out.println("next: " + inSetString);
-
if ( i >= 1 && i <= 12 )
-
{
-
monthNumber = i;
-
}
-
}
-
catch (NumberFormatException 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 NumberFormatException("Invalid month name string: " + inSetString);
-
}
-
}
-
if ( monthNumber == -1 )
-
{
-
throw new NumberFormatException("Illegal month Number: " + inSetString);
-
}
-
That looks like it would use the correct System.out.print() messages. But the problem is that I've been required to use IllegalArgumentException.
Just change the NumberFormatException to IllegalArgumentException.
Ok, that works. I see what the problem was now. Thanks a lot.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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:
...
|
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...
| |
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...
|
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"...
|
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...
|
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)
{
|
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....
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |