【发布时间】: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