【问题标题】:Throwing a custom NumberFormatException in Java在 Java 中抛出自定义 NumberFormatException
【发布时间】:2011-11-08 15:34:49
【问题描述】:

在将字符串月份转换为整数时,我试图抛出自己的 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.");*/

【问题讨论】:

  • 您注释掉的代码发生了什么?
  • @Thom 我在抛出后收到任何代码无法访问的声明。
  • 我取出了 throw 语句并将我自己的 String 添加到我之前在代码中捕获异常的位置。

标签: java throw numberformatexception


【解决方案1】:

类似这样的:

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);
}

编辑:现在您已经更新了您的帖子,看起来您实际上需要的是类似 parse(String) 的 SimpleDateFormat

【讨论】:

  • 第二个例子很好,因为它将异常链接在一起,因此您可以看到非法 Args 的原因是数字格式异常。
  • @unbeli 我取出了 throw 语句,并将我自己的 String 添加到我在代码中早些时候捕获异常的位置。 catch (NumberFormatException NFE) { System.out.println(NFE.getMessage() + " has non numeric input in entry");} 感谢您的输入
【解决方案2】:
try {
   // converts the month to an integer
   intmm = Integer.parseInt(mm);
} catch (NumberFormatException e) {
  throw new NumberFormatException("The month entered, " + mm+ " is invalid.");
}

【讨论】:

  • 是的,你是对的,因为如果引发 RuntimeException ...但在这种情况下概率很低。不过你是对的。
  • 我取出了 throw 语句,并将我自己的 String 添加到我之前在代码中捕获异常的位置。 catch (NumberFormatException NFE) { System.out.println(NFE.getMessage() + " has non numeric input in entry");} 感谢您的输入!
【解决方案3】:

Integer.parseInt() 已经产生 NumberFormatException。在您的示例中,抛出 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);
}

【讨论】:

  • 我取出了 throw 语句,并将我自己的 String 添加到我之前在代码中捕获异常的位置。 catch (NumberFormatException NFE) { System.out.println(NFE.getMessage() + " has non numeric input in entry");} 感谢您的输入
猜你喜欢
  • 1970-01-01
  • 2015-04-25
  • 2013-03-29
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 2020-01-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多