异常分类:
1.Error(JVM虚拟机错误)
2.Exception(异常):程序可以处理的异常
根据编译特性分类:
(1)检查性异常:在编译的时候必须检查,如IOException
(2)非检查性异常:在编译时可以不检查,如RuntimeException
根据运行时特性分类:
(1)运行时异常:不用捕捉的异常,如RuntimeException
(2)非运行时异常:需要捕捉,检查,如IOException
异常处理try catch finally
1.try{}//可能发生异常的代码
2.catch // 捕捉异常,可以是0~n个,0个catch的时候,try中不可以有检查性异常
3.finally // 最后一定会执行的
特殊情况分析:
1.
int x;
try{
x =2;
return x;
}finally{
x = 4;
}
返回的结果还是2,因为在返回的时候return的值x先做备份,然后再执行finally
2.
try{
return 2;
}finally {
return 4;
}
返回的结果是4,finally的返回会覆盖try中的
3.
try{
throw new NullPointerException();
}catch (Exception e){
throw new ArrayIndexOutOfBoundsException();
}
finally {
throw new IOException();
}
抛出的异常是IOException