// 第一个
try {
float j = 1 / 0;} catch (Exception e) {
exceptions.add(e);
}
//第二个
try{
String s = null;
String m = s.toLowerCase();} catch(Exception e){
exceptions.add(e);
}
//第三个
try {//3
int[] m = new int[2];
m[8] = 5;
}catch (Exception e) {
exceptions.add(e);
}
//第四个
try{//4
ArrayList<String> list = new ArrayList<String>();
String s = list.get(18);
}catch(Exception e){
exceptions.add(e);
}//第五个
try{//6
int num = Integer.parseInt("XYZ");
System.out.println(num);
}catch(Exception e){
exceptions.add(e);}
//第六个
FileReader reader = null;
try {
reader = new FileReader("d:/a.txt");
char temp = (char) reader.read();
System.out.println("读出的内容:" + temp);}catch(Exception e){
exceptions.add(e);
}
//第七个try{
FileWriter fw = new FileWriter("demo.txt");
fw.write("abcdefg");
fw.close();
}catch(Exception e){
exceptions.add(e);
}
//第八个
try {
Object x[] = new String[3];x[0] = new Integer(0);
} catch (Exception e) {
exceptions.add(e);
}
//第九个
SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
Date date;
try {
date = sdf.parse("");
} catch (Exception e) {
exceptions.add(e);
}
//第十个
try {
Object x = new String("String");
System.out.println((Integer) x);
} catch (Exception e) {
exceptions.add(e);
}其异常分别为:
java.lang.ArithmeticException: / by zero
java.lang.NullPointerException
java.lang.ArrayIndexOutOfBoundsException: 8
java.lang.IndexOutOfBoundsException: Index: 18, Size: 0
java.lang.NumberFormatException: For input string: "XYZ"
java.io.FileNotFoundException: d:/a.txt (No such file or directory)
java.security.AccessControlException: access denied ("java.io.FilePermission" "demo.txt" "write")
java.lang.ArrayStoreException: java.lang.Integer
java.text.ParseException: Unparseable date: ""
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
十个常见异常实例
最新推荐文章于 2022-02-16 14:35:42 发布