【问题标题】:How to continue executing a java program after an Exception is thrown?抛出异常后如何继续执行java程序?
【发布时间】:2012-03-08 22:20:05
【问题描述】:

我的示例代码如下:

public class ExceptionsDemo {

    public static void main(String[] args) {
        try {
            int arr[]={1,2,3,4,5,6,7,8,9,10};
            for(int i=arr.length;i<10;i++){
                if(i%2==0){
                    System.out.println("i =" + i);
                    throw new Exception();
                }            
            }
        } catch (Exception e) {
            System.err.println("An exception was thrown");
        }            
    }
}

我的要求是,在捕获到异常后,我想处理数组的剩余元素。我该怎么做?

【问题讨论】:

  • 为什么一开始就抛出异常?
  • 那么首先,如果您不想对异常做任何事情,为什么要抛出异常?
  • 将 try/catch 重新定位到循环内部。那么当异常发生时,由于你还在循环中,所以继续正常执行。
  • 如果你想继续,为什么要抛出这样的异常?
  • for(int i=arr.length;i&lt;10;i++) 你也进入这个循环吗?我想你的意思是for(int i=0; i&lt;arr.length;++i)

标签: java arrays exception exception-handling


【解决方案1】:

在 for 循环中移动 try catch 块,然后它应该可以工作

【讨论】:

    【解决方案2】:

    您需要稍微重新构造它,以便 try/catch 位于 for 循环内,而不是封闭它,例如

    for (...) {
      try {
        // stuff that might throw
      }
      catch (...) {
        // handle exception
      }
    }
    

    顺便说一句,你应该避免使用异常来进行流控制——异常应该用于异常的事情。

    【讨论】:

      【解决方案3】:

      您不能这样做,因为您的数组是在 try 子句中定义的。如果您希望能够访问它,请将其移出那里。另外,也许您应该以某种方式将我导致的异常存储在异常中,以便您可以继续。

      【讨论】:

        【解决方案4】:

        只要不抛出异常,那么:

        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        for (int i = 0; i < arr.length; i++) {
            if (i % 2 == 0) {
                System.out.println("i = " + i);
            }  
        }    
        

        或者抛出它,然后在循环内部而不是外部捕获它(但我看不出在这个简单的例子中抛出异常的意义):

        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        for (int i = 0; i < arr.length; i++) {
            try {
                if (i % 2 == 0) {
                    System.out.println("i = " + i);
                    throw new Exception();
                }
            }
            catch (Exception e) {
                System.err.println("An exception was thrown");
            }
        }
        

        旁注:看看代码在正确缩进时如何更容易阅读,并且在运算符周围包含空格。

        【讨论】:

          【解决方案5】:

          您的代码应如下所示:

          public class ExceptionsDemo {
          
              public static void main(String[] args) {
                  for (int i=args.length;i<10;i++){
                      try {
                          if(i%2==0){
                              System.out.println("i =" + i);
                              throw new Exception();  // stuff that might throw
                          }
                      } catch (Exception e) {
                          System.err.println("An exception was thrown");
                      }
                  }
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2015-10-31
            • 2012-04-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-05-13
            • 1970-01-01
            相关资源
            最近更新 更多