Throwing a custom NumberFormatException in Java
Asked Answered
C

3

7

I am trying to throw my own NumberFormatException when convrting a String month into an Integer. Not sure how to throw the exception. Any help would be appreciated. Do I need to add a try-catch before this part of the code? I already have one in another part of my code.

// sets the month as a string
mm = date.substring(0, (date.indexOf("/")));
// sets the day as a string
dd = date.substring((date.indexOf("/")) + 1, (date.lastIndexOf("/")));
// sets the year as a string
yyyy= date.substring((date.lastIndexOf("/"))+1, (date.length()));
// converts the month to an integer
intmm = Integer.parseInt(mm);
/*throw new NumberFormatException("The month entered, " + mm+ is invalid.");*/
// converts the day to an integer
intdd = Integer.parseInt(dd);
/* throw new NumberFormatException("The day entered, " + dd + " is invalid.");*/
// converts the year to an integer
intyyyy = Integer.parseInt(yyyy);
/*throw new NumberFormatException("The yearentered, " + yyyy + " is invalid.");*/
Clanton answered 8/11, 2011 at 15:34 Comment(3)
What's happening with the code that you commented out?Tega
@Tega I receive a unreachable statment for any code after the throw.Clanton
I took out the throw statements and added my own String to where I was catching the exception earlier in my code.Clanton
E
6

something like this:

try {
    intmm = Integer.parseInt(mm);
catch (NumberFormatException nfe) {
    throw new NumberFormatException("The month entered, " + mm+ " is invalid.");
}

Or, a little bit better:

try {
    intmm = Integer.parseInt(mm);
catch (NumberFormatException nfe) {
    throw new IllegalArgumentException("The month entered, " + mm+ " is invalid.", nfe);
}

EDIT: Now that you've updated your post, it seems like what you actually need is something like parse(String) of SimpleDateFormat

Eldoria answered 8/11, 2011 at 15:38 Comment(2)
The second example is nice because it will chain together the exceptions, so you can see that the cause of the Illegal Args was the Number Format Exception.Pya
@Eldoria I took out the throw statements and added my own String to where I was catching the exception earlier in my code. catch (NumberFormatException NFE) { System.out.println(NFE.getMessage() + " has non numeric input in entry");} Thanks for your inputClanton
S
3
try {
   // converts the month to an integer
   intmm = Integer.parseInt(mm);
} catch (NumberFormatException e) {
  throw new NumberFormatException("The month entered, " + mm+ " is invalid.");
}
Sportswear answered 8/11, 2011 at 15:37 Comment(2)
Yes, you are right because if raised a RuntimeException ... But in this context the probability are low. However you are right.Sportswear
I took out the throw statements and added my own String to where I was catching the exception earlier in my code. catch (NumberFormatException NFE) { System.out.println(NFE.getMessage() + " has non numeric input in entry");} Thanks for your input!Clanton
I
1

Integer.parseInt() already produces NumberFormatException. In you example it seems more appropriate to throw IllegalArgumentException:

int minMonth = 0;
int maxMonth = 11;
try 
{
    intmm = Integer.parseInt(mm);
    if (intmm < minMonth || intmm > maxMonth) 
    {
      throw new IllegalArgumentException
      (String.format("The month '%d' is outside %d-%d range.",intmm,minMonth,maxMonth));
    }
}
catch (NumberFormatException nfe) 
{
  throw new IllegalArgumentException
  (String.format("The month '%s'is invalid.",mm) nfe);
}
Involucre answered 8/11, 2011 at 15:51 Comment(1)
I took out the throw statements and added my own String to where I was catching the exception earlier in my code. catch (NumberFormatException NFE) { System.out.println(NFE.getMessage() + " has non numeric input in entry");} Thanks for your inputClanton

© 2022 - 2024 — McMap. All rights reserved.