五个最基本常见异常例子
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);
} 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 {
}
学习学习 System.out.println("楼主的帖子异常") 火绒可以分析 System.out.println("楼主的帖子非常异常") 谢谢分享!
页:
[1]