Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中引发自定义NumberFormatException_Java_Throw_Numberformatexception - Fatal编程技术网

在Java中引发自定义NumberFormatException

在Java中引发自定义NumberFormatException,java,throw,numberformatexception,Java,Throw,Numberformatexception,在将字符串转换为整数时,我试图抛出自己的NumberFormatException。不确定如何抛出异常。任何帮助都将不胜感激。我需要在这部分代码之前添加一个try-catch吗?我的代码的另一部分中已经有一个 // sets the month as a string mm = date.substring(0, (date.indexOf("/"))); // sets the day as a string dd = date.substring((date.indexOf("/")) +

在将字符串转换为整数时,我试图抛出自己的NumberFormatException。不确定如何抛出异常。任何帮助都将不胜感激。我需要在这部分代码之前添加一个try-catch吗?我的代码的另一部分中已经有一个

// 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.");*/
大概是这样的:

try {
    intmm = Integer.parseInt(mm);
catch (NumberFormatException nfe) {
    throw new NumberFormatException("The month entered, " + mm+ " is invalid.");
}
或者,更好一点:

try {
    intmm = Integer.parseInt(mm);
catch (NumberFormatException nfe) {
    throw new IllegalArgumentException("The month entered, " + mm+ " is invalid.", nfe);
}
编辑:既然您已经更新了帖子,那么您实际需要的似乎是类似SimpleDataFormat的东西。在您的示例中,抛出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);
}
int minMonth=0;
int maxMonth=11;
尝试
{
intmm=Integer.parseInt(mm);
如果(intmmmaxMonth)
{
抛出新的IllegalArgumentException
(String.format(“月份'%d'超出了%d-%d范围。”,intmm,minMonth,maxMonth));
}
}
捕获(NumberFormatException nfe)
{
抛出新的IllegalArgumentException
(String.format(“月份'%s'无效。”,mm)nfe);
}

您注释掉的代码发生了什么情况?@Thom抛出后,我收到了任何代码都无法访问的声明。我取出了抛出语句,并将自己的字符串添加到代码中先前捕获异常的位置。第二个示例很好,因为它将异常链接在一起,因此,您可以看到,非法参数的原因是数字格式异常。@unbeli我取出了throw语句,并将自己的字符串添加到代码前面捕获异常的位置。catch(NumberFormatException NFE){System.out.println(NFE.getMessage()+“在条目中有非数字输入”);}感谢您的输入是的,您是对的,因为如果引发运行时异常。。。但在这种情况下,可能性很低。然而,你是对的,我取出了throw语句,并在代码前面捕获异常的地方添加了我自己的字符串。catch(NumberFormatException NFE){System.out.println(NFE.getMessage()+“在条目中有非数字输入”);}谢谢您的输入!我取出了throw语句,并将自己的字符串添加到代码前面捕获异常的位置。catch(NumberFormatException NFE){System.out.println(NFE.getMessage()+“条目中有非数字输入”);}谢谢您的输入
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);
}