
其中 Error 和 RuntimeException 是非检查异常(Unchecked Exception)不要求在代码中存在相应的处理逻辑,与之相反的则是检查异常(Checked Exception).
需要注意的是异常处理中的 finally 和 return 的处理逻辑,try 或 catch 中的 return 之后如果有 finally 的逻辑,那么 return 会先被保存起来,注意 return 的结果已经被保存起来,finally 对相关变量的修改是无效的,例如:
public class Main {
public static void main(String[] args) {
int ret = f();
System.out.println(ret);
}
private static int f(){
int a = 0;
try{
return a;
} finally {
a = 1;
}
}
}
返回的结果为 0,而非 1,如果 finally 中有 return 那么最终会使用 finally 中的 return。