注册 登录
编程论坛 JAVA论坛

异常处理

yu1543054075 发布于 2015-06-03 20:37, 369 次点击
public class CatchWho2 {
    public static void main(String[] args) {
        try{
            try {
                throw new ArrayIndexOutOfBoundsException();
            }
            catch(ArithmeticException e) {
                System.out.println(
                  "ArrayIndexOutOfBoundsException" +
                  "/内层 try...catch");
                }
                throw new ArithmeticException();
            }
            catch(ArithmeticException e) {
                System.out.println("发生ArithmeticException");
            }
            catch(ArrayIndexOutOfBoundsException e) {
                System.out.println(
                   "ArrayIndexOutOfBoundsException" +
                   "/外层 try...catch");
                 }
                }
            }
结果显示为:
ArrayIndexOutOfBoundsException/外层 try...catch
为什么没有显示“发生ArithmeticException”
3 回复
#2
林月儿2015-06-03 21:15
程序代码:
public class CatchWho2 {
    public static void main(String[] args) {
        try{
            System.out.println(1/0);
            try {
                throw new ArrayIndexOutOfBoundsException();
            }
            catch(ArithmeticException e) {
                System.out.println(
                  "ArrayIndexOutOfBoundsException" +
                  "/内层 try...catch");
                }
                throw new ArithmeticException();
            }
            catch(ArithmeticException e) {
                System.out.println("发生ArithmeticException");
            }
            catch(ArrayIndexOutOfBoundsException e) {
                System.out.println(
                   "ArrayIndexOutOfBoundsException" +
                   "/外层 try...catch");
                 }
                }
            }
#3
yu15430540752015-06-04 14:22
版主能够给我讲解一下吗?我的程序为什么没有显示“发生ArithmeticException”, throw new ArithmeticException();这里扔出的异常不是应该被下面的 catch(ArithmeticException e)所捕捉么?
还有为什么你添加了一个 System.out.println(1/0);就行了呢
#4
林月儿2015-06-04 17:31
因为加了就会发生 ArithmeticException异常啊,这里的分支相当于instanceof判断,遇到合适的异常情况执行合适的分支处理
1