A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Tauruszzy 中级黑马   /  2015-5-9 14:48  /  590 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/*
当可能产生的异常不止一个时可以采用多异常处理方式,下面就是一个多异常处理
*/
class Demo
{
        int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException
        {
                int[] arr=new int[a];
                System.out.println("数组的第一个元素是:"+arr[0]);//第2处
                return a/b;
        }
}

class ExceptionDemo
{
        public static void main(String[]args)
        {
                Demo d=new Demo();
                try
                {
                        int x =d.div(4,1);//第一处
                    System.out.println("x="+x);
                }
                catch(ArithmeticException e)
                {
            System.out.println(e.toString());
            System.out.println("运算错误");
                }
                catch (ArrayIndexOutOfBoundsException e)
                {
                        System.out.println(e.toString());
                        System.out.println("数组越界");
                }
                System.out.println("over");
        }
}
备注:因为程序没有错误,所以不执行catch语句,直接输出:
数组的第一个元素是:0
x=4
over

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
别的地方不动,只把上面的第一处改为int x =d.div(4,0);程序输出:
数组的第一个元素是:0
java.lang.ArithmeticException:/ by zero
运算错误
over
*/
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
如果把第2处改为System.out.println("数组的第一个元素是:"+arr[4]);,则程序显示:
java.lang.ArrayIndexOutOfBoundsExceptionException:
数组越界
over
*/
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
两个catch不会同时执行,因为当在下列程序
int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException
        {
                int[] arr=new int[a];
                System.out.println("数组的第一个元素是:"+arr[0]);//第2处
                return a/b;
        }
中产生错误的时候,虚拟机会跳转到相对应的try中进而执行相对应的catch语句
*/

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class Demo
{
        int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException
        {
                int[] arr=new int[a];
                System.out.println("数组的第一个元素是:"+arr[4]);
                return a/b;
        }
}

class ExceptionDemo
{
        public static void main(String[]args)
        {
                Demo d=new Demo();
                try
                {
                        int x =d.div(4,1);
                    System.out.println("x="+x);
                }
                catch(Exception e)//这个的出现导致下面两个catch语句的多余,程序会报错
                {
            System.out.println(e.toString());
            System.out.println("我是Exception!!!");
                }
                catch(ArithmeticException e)
                {
            System.out.println(e.toString());
            System.out.println("运算错误");
                }
                catch (ArrayIndexOutOfBoundsException e)
                {
                        System.out.println(e.toString());
                        System.out.println("数组越界");
                }
                System.out.println("over");
        }
}
备注:当执行上面语句的时候,会提示错误,错误信息的意思是已捕获到错误信息ArrayIndexOutOfBoundsExceptionException和 ArithmeticException,这是因为这两个类是Exception类的子类,父类中已经封装了这两个子类的对象,再定义两个子类对象多余。

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class Demo
{
        int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException
        {
                int[] arr=new int[a];
                System.out.println("数组的第一个元素是:"+arr[4]);
                return a/b;
        }
}

class ExceptionDemo
{
        public static void main(String[]args)
        {
                Demo d=new Demo();
                try
                {
                        int x =d.div(4,1);
                    System.out.println("x="+x);
                }
               
                catch(ArithmeticException e)
                {
            System.out.println(e.toString());
            System.out.println("运算错误");
                }
                catch (ArrayIndexOutOfBoundsException e)
                {
                        System.out.println(e.toString());
                        System.out.println("数组越界");
                }
                catch(Exception e)//这个catch语句不会执行,因为上面有更为详细的对象执行
                {
            System.out.println(e.toString());
            System.out.println("我是Exception!!!");
                }
                System.out.println("over");
        }
}
备注:注意这个情况

----------------------
----------------------
总结:在定义异常的时候要定义的尽量具体,因为更容易发现错误,给出提示。
         声明有几个异常,就应有几个catch块。不要定义多余的catch块。
         如果多个catch块中的异常出现继承关系,子类异常catch块放在上面,父类异常catch块放在最下面。
         建议在异常catch块中定义具体的处理方式。

3 个回复

倒序浏览
总结的不错,加油!
回复 使用道具 举报
Dracove 发表于 2015-5-9 16:04
总结的不错,加油!

谢谢鼓励,共勉
回复 使用道具 举报
学习了,不错
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马