可是
catch(ArrayIndexOutOfBoundsException e){ //捕获数组下标越界异常
System.out.println("Array index oob:"+e);
}
并没有报错呀
能说说数组下标的越界问题吗
catch(ArrayIndexOutOfBoundsException e){ //捕获数组下标越界异常
System.out.println("Array index oob:"+e);
}
并没有报错呀
能说说数组下标的越界问题吗
{
public static void main(String[]grav)
{
try{
int c[] = {1};
System.out.println(c[4]);
}catch(ArrayIndexOutOfBoundsException e){ //捕获数组下标越界异常
System.out.println("Array index oob:"+e);
} }
}
怎么不会,你运行一下这个试试
但请问为什么不调用
System.out.println(c[4]);
就不出现异常错误
请指教
请问下面的类为什么不调用class ExceptionDemo2{
public static void main(String args[]){
try{
int a=args.length;
System.out.println("a="+a);
int b=42/a;
int c[]={1};
c[4]=99;
}
catch(ArithmeticException e){
System.out.println("Divide by 0:"+e);
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Array index ob:"+e);
}
System.out.println("After try/catch blocks");
}
}
这后就出错,转到了catch(ArithmeticException e){
System.out.println("Divide by 0:"+e);
}
根本就没运行int c[]={1};c[4]=99;
所以没有打出Array index ob:"+e
都会触发异常.上面这个类会触发 ArithmeticException 异常(如果你不输入命令行参数)
如果输入命令行参数会触发ArrayIndexOutOfBoundsException异常当捕获多个异常时,只能捕获到一个异常,所以在写捕获多个异常的语句时,要注意异常捕获顺序。