前言
java异常类型分为Error(错误)和Exception。Exception又包含运行时异常(Runtime Exception 又叫非检测异常)和非运行异常(又叫检测异常)
1.Error
Error是程序处理不了的,例如OutOfMemoryError等,这些异常发生时jvm会停止运行
2.Exception
Exception 分为运行时异常和检测异常,运行时异常指程序在运行过程中发生的异常如:NullPointerException,IndexOutOfBoundsException这些异常可以处理也可以不处理,一般来说程序要避免出现运行时异常,检测异常指编写代码时会检测到的异常如:IOException、SQLException等需要处理或则抛出异常否则程序无法继续进行
3.异常捕获机制
异常捕获机制 try…catch…finally,代码如下
BufferedReader reader = null;
String filename = "c:/work/test.csv";
try {
InputStreamReader isr = new InputStreamReader(new FileInputStream(filename), "utf-8");
reader = new BufferedReader(isr);
reader.readLine();//跳过第一行
String line = null;
while((line = reader.readLine()) != null){
......
}
} catch (IOException e) {
e.printStackTrace();
}finally{
reader.close();
}