public class Test{
public Test() {
}
public static void main(String args[])throws ArrayIndexOutOfBoundsException{
int arr[]=new int[5];
arr[10]=10;
System.out.println("main()方法结束");
}
}
//result
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Test.main(Test.java:14)
public class Test{
public Test() {
}
public static void main(String args[]){
try{
int arr[]=new int[5];
arr[10]=10;
}catch(ArrayIndexOutOfBoundsException ex){
ex.printStackTrace();
}finally{
System.out.println("这里一定会被执行");
}
System.out.println("main()方法结束");
}
}
public class Test{
public Test() {
}
public static void main(String args[]){
int arr[]=new int[5];
try{
setZero(arr,10);
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
System.out.println("main 方法结束");
}
public static void setZero(int arr[],int index)
throws ArrayIndexOutOfBoundsException
{
arr[index]=0;
}
}
//result:
java.lang.ArrayIndexOutOfBoundsException: 10
at Test.setZero(Test.java:24)
at Test.main(Test.java:15)
main 方法结束
public class Test{
public Test() {
}
public static void main(String args[])throws ArrayIndexOutOfBoundsException{
try{
throw new ArrayIndexOutOfBoundsException("\n我是个性化的异常信息:\n数组下标越界");
}catch(ArrayIndexOutOfBoundsException ex){
System.out.println(ex);
}
}
}
//关键字throws和throw的配合使用