-4
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 块一次。有没有办法在单个方法中抛出多个异常?

4

7 回答 7

3

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

confusion c=new confusion(2);
confusion c1=new confusion(0);
于 2013-08-27T08:47:27.973 回答
2
confusion c=new confusion(2);
confusion c1=new confusion(0);

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

于 2013-08-27T08:47:07.380 回答
1

这是因为您正在创建两个对象c1,并且c

于 2013-08-27T08:48:06.930 回答
1

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

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

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

于 2013-08-27T08:48:44.737 回答
1

您在尝试中调用代码两次。

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

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

Executing finally block in code

如果你再叫它,

confusion c1=new confusion(3);

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

于 2013-08-27T08:48:50.743 回答
1

您正在使用以下代码创建两个对象。这就是 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);
} 

现在 finally 块调用了 4 次。

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

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

于 2013-08-27T08:52:19.947 回答
0

因为您正在创建两个 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");
        }
     }
  }
于 2013-08-27T09:00:30.057 回答