抛出错误的异常
我正在向此方法传递一个字符串。该字符串是“44”。然后我将其解析为 int 并执行 if 语句来查看它是否在 1 到 12 之间。然后它转到 else 语句,其中我有 - throw new IllegalArgumentException("此处非法月份数:" + inSetString);
由于某种原因,这不会被抛出,而是会进入下一个异常并抛出该异常 - throw new IllegalArgumentException("无效的月份名称字符串:" + inSetString);
为什么它抛出第二个异常语句而不是第一个?它确实转到 else 语句。我已经通过使用 - System.out.println(" except" + inSetString); 进行了测试
这是我的代码:
public void setMonthName(String inSetString)
{
int i = 0;
monthNumber = -1;
try
{
System.out.println("first" + inSetString);
i = Integer.parseInt(inSetString);
System.out.println("next" + inSetString);
if (i >= 1 && i <= 12)
{
monthNumber = i;
}
else
{
System.out.println("except" + inSetString);
throw new IllegalArgumentException("Illegal month number here: " + inSetString);
}
}
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);
}
}
I'm passing in a string to this method. The string is "44". I then parse it into an int and do an if statement to see if it's between 1 and 12. It then goes to the else statement where I have - throw new IllegalArgumentException("Illegal month number here: " + inSetString);
For some reason, that doesn't get thrown and it goes down to the next exception and that one is thrown - throw new IllegalArgumentException("Invalid month name string: " + inSetString);
Why does it throw the second exception statement but not the first? It DOES go to the else statement. I've tested that by using - System.out.println("except" + inSetString);
Here's my code:
public void setMonthName(String inSetString)
{
int i = 0;
monthNumber = -1;
try
{
System.out.println("first" + inSetString);
i = Integer.parseInt(inSetString);
System.out.println("next" + inSetString);
if (i >= 1 && i <= 12)
{
monthNumber = i;
}
else
{
System.out.println("except" + inSetString);
throw new IllegalArgumentException("Illegal month number here: " + inSetString);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
异常被抛出,但随后你有一个 catch 块,它不执行任何操作并允许继续执行。然后,您遇到另一个异常,该异常导致程序崩溃。
尝试在 catch 块中添加一些日志记录(或
e.printStackTrace()
),您将看到会发生什么。另外,尝试使用 IDE 的调试器而不是添加打印语句。您将对程序的执行有更好的了解。
The exception is thrown, but then you have a catch block that doesn't do anything and allows the execution to continue. You then reach the other exception and that one crashes the program.
Try adding some logging (or
e.printStackTrace()
) in your catch block, you'll see what happens.Also, try using your IDE's debugger instead of adding print statements. You'll have a better understanding of your program's execution.