**
7-1 jmu-Java-06异常-01-常见异常(pta)
**
自己编码以产生常见异常。
###main方法:
事先定义好一个大小为5的数组。
根据屏幕输入产生相应异常
提示:可以使用System.out.println(e)打印异常对象的信息,其中e为捕获到的异常对象。
**输入说明: **
arr 代表产生访问数组是产生的异常。然后输入下标,如果抛出ArrayIndexOutOfBoundsException异常则显示,如果不抛出异常则不显示。
null,产生NullPointerException
cast,尝试将String对象强制转化为Integer对象,产生ClassCastException。
num,然后输入字符,转化为Integer,如果抛出NumberFormatException异常则显示。
其他,结束程序。
输入样例:
arr 4
null
cast
num 8
arr 7
num a
other
输出样例:
java.lang.NullPointerException
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
java.lang.ArrayIndexOutOfBoundsException: 7
java.lang.NumberFormatException: For input string: "a"
答案
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int []arr = new int [5];
Scanner input = new Scanner(System.in);
while (input.hasNext()){
String a = input.next();
if (a.equals("arr")){
int b = input.nextInt();
try {
int d = arr[b];
}catch (Exception e){
System.out.println(e.toString());
}
}else if (a.equals("num")){
String b = input.next();
try{
int d = Integer.parseInt(b);
}catch (Exception e){
System.out.println(e.toString());
}
}else if (a.equals("cast")){
try {
Object str = new String("cc");
System.out.println((Integer)(str));
}catch (Exception e){
System.out.println(e.toString());
}
}else if (a.equals("null")){
try {
String t = null;
int l = t.length();
}catch (Exception e){
System.out.println(e.toString());
}
}else {
System.exit(0);
}
}
}
}