Exception:
如果执行程序时,当遇到程序异常时,则终止程序的执行
1.编译时异常
2.运行时异常
常见运行异常
1)空指针异常:NullPointerException
eg:
**public class Test {
public static void main(String[] args){
String str = new String(“AAA”);
str=null;
System.out.println(str.length());
}
}**
2)类型转换异常:ClassCastException
eg:
public class Test {
public static void main(String[] args){
Object obj = new Object();
String str = (String) obj;
}
}
3.算术异常:ArithmeticException
eg:
public class Test {
public static void main(String[] args){
int i=10;
System.out.println(i/0);
}
}
4)数组下标越界异常:ArrayIndexOutOfBoundsException
eg:
public class Test {
public static void main(String[] args){
int i[]=new int[10];
System.out.println(i[11]);
}
}