| class Demo//该程序编译失败。原因:多个catch情况下,异常的父类写在了异常子类的前面。 {
 int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException
 {
 
 int[] arr = new int[a];
 
 System.out.println(arr[4]);
 
 return a/b;
 }
 }
 
 
 class  ExceptionDemo2
 {
 public static void main(String[] args) //throws Exception
 {
 Demo d = new Demo();
 try
 {
 int x = d.div(5,0);
 System.out.println("x="+x);
 }
 
 catch(Exception e)
 {
 System.out.println("hahah:"+e.toString());
 }                catch (ArithmeticException e)
 {
 System.out.println(e.toString());
 }
 catch (ArrayIndexOutOfBoundsException e)
 {
 System.out.println(e.toString());
 }
 
 
 
 System.out.println("over");
 
 }
 }
 /*在异常中可以灵活使用,可以将两种异常子类ArithmeticException,ArrayIndexOutOfBoundsException
 都通过Exception接收并且处理。此程序在正常情况下就出现两种异常一个数组角标越界异常,一个运行时异常
 ,这样如果有针对这两个异常的处理就可以省略父类的异常。当然也直接用父类异常顶替两个子类异常。
 */
 
 |