好友
阅读权限10
听众
最后登录1970-1-1
|
public class HomeWork01 {
public static void main(String[] args) {
System.out.println("数学异常.");
try {
int a = 1 / 0;
System.out.println(a);
} catch (ArithmeticException exception) {
System.out.println("异常了");
} finally {
System.out.println("数学异常结束.");
}
/***************************************************************/
System.out.println("空指针异常");
try {
String string = new String();
string = null;
System.out.println(string.length());
} catch (NullPointerException exception) {
System.out.println("异常了");
} finally {
System.out.println("空指针异常结束.");
}
/***************************************************************/
System.out.println("数学格式异常");
try {
String str = "aaa123";
Integer integer = new Integer(str);
System.out.println(integer);
} catch (NumberFormatException exception) {
System.out.println("异常了");
} finally {
System.out.println("数学格式异常结束");
}
/***************************************************************/
System.out.println("索引越界异常");
try {
int a[] = { 0, 1 };
System.out.println(a[2]);
} catch (ArrayIndexOutOfBoundsException exception) {
System.out.println("异常了");
} finally {
System.out.println("索引越界异常结束");
}
/***************************************************************/
System.out.println("类型转换异常");
try {
A a = new C();
B b = (B) a;
System.out.println(b);
} catch (ClassCastException exception) {
System.out.println("异常了");
} finally {
System.out.println("类型转换异常结束");
}
}
}
class A {
}
class B extends A {
}
class C extends A {
}
|
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|