1. 传统的 try catch 模块
对于 IO操作,数据库连接,socket连接使用资源关闭时,一般进行的try catch 进行异常捕获和释放资源,示例代码:
/**
* 释放资源
*
* @author www.codingme.net
*/
public class TryCatachFinally {
/**
* 异常处理
*
* @param args
*/
public static void main(String[] args) throws Exception {
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream("jdk-feature-7.iml");
} catch (FileNotFoundException e) {
throw e;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
throw e;
}
}
}
}
}
以上的代码也会出现严重的bug,也就是多个异常出现不会都捕捉,会有覆盖的情况出现,示例:
package net.codingme.feature.jdk7;
import java.io.IOException;
/**
* 释放资源
*
* @author www.codingme.net
*/
public class TryCatachFinallyThrow {
/**
* 异常处理
*
* @param args
*/
public static void main(String[] args) throws Exception {
read();
}
public static void read() throws Exception {
FileRead fileRead