1.异常:程序无法正常执行完毕
异常体系:
Throwable
/
Error Exception
2.程序出错分为错误和异常
Error : 错误,一般由虚拟机生成并脱出,无需要程序猿管理
Exception : 异常
CheckedException 编译时异常|检查时异常: 发生在程序编译期间
程序如果遇到编译时异常没有处理,程序无法正常运行
RuntimeException 运行时异常 : 发生程序运行期间
一般可以通过增强程序健壮性的代码处理 if
注意:如果程序遇到异常没有处理,无法继续执行
3.常见的一些运行时异常
1.空指针异常 NullPointerException
2.数组越界异常 ArrayIndexOutOfBoundsException
3.数组长度负数异常 NegativeArraySizeException
4.类型转换异常 ClassCastException
5.数学异常 ArithmeticException
6.数字格式异常 NumberFormatException
public class Class001_Exception {
public static void main(String[] args) {
int i = -3;
//增强程序健壮性的代码
if(i>0){
int[] arr = new int[i];
System.out.println(arr[5]);
}
Object obj = new String();
if(obj instanceof Person){
Person p = (Person)obj;
}
//System.out.println(5/0);
String str = "12abc3";
//System.out.println(Integer.valueOf(str));
//InputStream is = new FileInputStream("");
}
}
class Person{}