【问题标题】:exception handling and finally block in javajava中的异常处理和finally块
【发布时间】:2013-08-27 08:44:35
【问题描述】:
public class Confusion {
    Confusion(int i) {
        int j = 5;
        int[] a = new int[2];
        try {
            a[0] = 4;
            if (i <= 0) {
                int k = j / i;
            } else {
                System.out.println(j / i);
            }
        } catch (ArithmeticException sa) {
            System.out.println("Wrong value" + sa);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("out of range massage in Class");
        } finally {
            System.out.println("Executing finally block in code");
        }
    }

    void k() {
        int[] a = new int[2];
        {
            try {
                a[4] = 4;
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("out of range");
            }
        }
    }
}

public class Nested {
    public static void main(String[] args) {
        Confusion c = new Confusion(2);
        Confusion c1 = new Confusion(0);
        c1.k();
        c.k();
    }
}

输出:

-2
Executing finally block in code
Wrong valuejava.lang.ArithmeticException: / by zero
Executing finally block in code
out of range
out of range

每当我执行下面代码中编写的finally{} 块时,它都会被执行两次。不知道为什么会这样。我只想运行 finally 块一次。 有没有办法在一个方法中抛出多个异常?

【问题讨论】:

  • 因为你调用了两次,不是吗?
  • 不用在构造函数中有逻辑并有这种“问题”,你可以将逻辑移动到一个方法中,这样就很简单了...... 2 方法调用 = 2 try{}catch{}finally{} 块跨度>

标签: java exception-handling try-catch-finally


【解决方案1】:

这是因为您有两个 Confusion 对象。因此,构造函数会被执行两次。

confusion c=new confusion(2);
confusion c1=new confusion(0);

【讨论】:

    【解决方案2】:
    confusion c=new confusion(2);
    confusion c1=new confusion(0);
    

    这就是为什么你最终得到 2 个输出。

    【讨论】:

      【解决方案3】:

      这是因为您正在创建两个对象 c1c

      【讨论】:

        【解决方案4】:

        finally 块总是在 try 块退出时执行。

        http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html

        所以不管有什么异常都会执行 finally 块。当你创建两个对象时,它会调用构造函数两次,最后也会执行两次。

        【讨论】:

          【解决方案5】:

          你在 try 中调用了代码两次。

          confusion c=new confusion(2);
          confusion c1=new confusion(0);
          

          这意味着每次调用 try 时都会发生 finally,它会打印...

          Executing finally block in code

          如果你再次调用它,

          confusion c1=new confusion(3);

          它会第三次打印出 finally 的内容。

          【讨论】:

            【解决方案6】:

            您正在使用以下代码创建两个对象。这就是 finally 块被执行两次的原因。

            confusion c=new confusion(2);
            confusion c1=new confusion(0);
            

            请试试这个

            public static void main(String[] args){
               confusion c=new confusion(2);
               confusion c1=new confusion(0);
               confusion c1=new confusion(10);
               confusion c1=new confusion(5);
            } 
            

            现在终于块被调用了 4 次。

            Java 中的构造函数是在创建对象时执行的代码块。

            请阅读构造函数参考http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

            【讨论】:

              【解决方案7】:

              因为您正在创建两个 Confusion 对象。因此finally块会被执行两次。

              confusion c=new confusion(2);
              confusion c1=new confusion(0);
              

              如果你只想最终执行一次。试试这个代码

               public class Confusion {
                  Confusion(int i) {
                      int j = 5;
                      int[] a = new int[2];
                      a[0] = 4;
                      if (i <= 0) {
                         int k = j / i;
                      } else {
                         System.out.println(j / i);
                      }
                  }
              
                  void k() {
                     int[] a = new int[2];
                          a[4] = 4;
                 }
              }
              
              public class Nested {
                 public static void main(String[] args) {
                    try{
                         Confusion c = new Confusion(2);
                         Confusion c1 = new Confusion(0);
                         c1.k();
                         c.k();
                       }catch (ArithmeticException sa) {
                          System.out.println("Wrong value" + sa);
                       } catch (ArrayIndexOutOfBoundsException e) {
                          System.out.println("out of range massage in Class");
                      } finally {
                          System.out.println("Executing finally block in code");
                      }
                   }
                }
              

              【讨论】:

                猜你喜欢
                • 2011-08-26
                • 2016-09-19
                • 1970-01-01
                • 2017-05-05
                • 2012-09-26
                • 2015-01-17
                • 2019-03-31
                • 2011-08-28
                • 2011-01-31
                相关资源
                最近更新 更多