异常捕获

来源:互联网 发布:网络平台代理招商 编辑:程序博客网 时间:2024/05/29 01:53
package test;


import java.util.Scanner;


public class ExceptionDemo {


public static void main(String[] args) {
// TODO Auto-generated method stub
    Scanner reader=new Scanner(System.in);
    int c;
    String s[]={"红色","黄色","蓝色","绿色"};
    System.out.println("你最喜欢下面哪种颜色?");
    for (int i=0;i<s.length;i++)
    System.out.println((i+1)+s[i]+" ");
    System.out.print("\n请选择:");
    try {
c=reader.nextInt();
System.out.println("你选择的是"+s[c-1]);
} catch (ArrayIndexOutOfBoundsException e) {
// TODO Auto-generated catch block
System.out.println("发生数组下标越界异常!"+e.toString());
}
    catch (Exception e){
    System.out.println(e.toString());
    }
}


}
原创粉丝点击