抛出错误的异常

发布于 2025-01-07 03:53:01 字数 1519 浏览 0 评论 0原文

我正在向此方法传递一个字符串。该字符串是“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛刀软妹 2025-01-14 03:53:01

异常被抛出,但随后你有一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文