Java 处理异常的语句try-catch-finally应该大家都不陌生, 程序中很多地方都会使用到, 比如IO操作, 对文件的读写, 数据库读写等, 另外事务处理也会用到, 在try块中处理逻辑, catch中回滚事务 ,finally中提交事务, 举个例子:
FileReadDemo.java
public class FileReadDemo {
public void readFile() throws IOException {
byte[] buff = new byte[1024];
FileInputStream input = null;
try {
input = new FileInputStream(new File("D:/test/itart.txt"));
while (-1 != input.read(buff)) {
System.out.println(new String(buff));
}
} catch (IOException e) {
throw e;
} finally {
if (null != input) {
try {
input.close();
} catch (IOException e) {
throw e;
}
}
}
}
public static void main(String[] args) {
try {
new FileReadDemo().readFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意: 上面的例子实际上存在异常丢失的隐患, 如果第一个try中出现异常, 接着在执行finally中的input.close()也出现异常, 这时main 方法只能接收到input.close的异常信息, 第一个异常会被覆盖, 导致异常信息丢失, 详见: try-catch-finally异常信息丢失
所以正确的写法如下:
public class FileReadDemo {
public void readFile() throws ApplicationException {
byte[] buff = new byte[1024];
IOException processException = null;
FileInputStream input = null;
try {
input = new FileInputStream(new File("D:/test/itart.txt"));
while (-1 != input.read(buff)) {
System.out.println(new String(buff));
}
} catch (IOException e) {
processException = e;
} finally {
try {
if(null != input){
input.close();
}
} catch (IOException e) {
if(null == processException){
throw new ApplicationException(e);
}else{
throw new ApplicationException("FileInputStream close exception", processException);
}
}
if(processException !=null){
throw new ApplicationException(processException);
}
}
}
public static void main(String[] args){
try {
new FileReadDemo().readFile();
} catch (ApplicationException e) {
e.printStackTrace();
}
}
}
这种写法是不是很糟糕, 我们实际关注的代码就只是第一个try中的四行代码, 这样的缺陷很明显, 一旦系统中有多处地方要用到类似的文件处理操作时, 就需要重复的做try-catch-finally, 很明显, 这就违背了程序开发中的一个重要原则: DRY(Don’t repeat yourself), 代码重复, 不容易阅读和维护, 同时也存在一个隐患, 忘记关闭, 异常没正确处理等, 引入模板方法就可以很好的解决这个问题.
咱们先对代码进行重构, 剥离业务代码和异常处理代码.
public class FileReadDemo {
public void readFile(String path) throws ApplicationException {
IOException processException = null;
FileInputStream input = null;
try {
input = new FileInputStream(new File(path));
process(input);
} catch (IOException e) {
processException = e;
} finally {
try {
if (null != input) {
input.close();
}
} catch (IOException e) {
if (null == processException) {
throw new ApplicationException(e);
} else {
throw new ApplicationException(
"FileInputStream close exception", processException);
}
}
if (processException != null) {
throw new ApplicationException(processException);
}
}
}
public void process(FileInputStream input) throws IOException {
byte[] buff = new byte[1024];
while (-1 != input.read(buff)) {
System.out.println(new String(buff));
}
}
public static void main(String[] args) {
try {
new FileReadDemo().readFile("D:/test/itart.txt");
} catch (ApplicationException e) {
e.printStackTrace();
}
}
}
可以看到, 在实际项目中每个功能对文件处理的差异地方就只有process方法, readFile中的异常处理是都一样的, 所以可以将readFile当成模板方法可以重复使用, process定义为抽象类, 由具体代码实现, 这样代码就可以变为:
public abstract class FileReadDemo {
public void readFile(String path) throws ApplicationException {
IOException processException = null;
FileInputStream input = null;
try {
input = new FileInputStream(new File(path));
process(input);
} catch (IOException e) {
processException = e;
} finally {
try {
if (null != input) {
input.close();
}
} catch (IOException e) {
if (null == processException) {
throw new ApplicationException(e);
} else {
throw new ApplicationException(
"FileInputStream close exception", processException);
}
}
if (processException != null) {
throw new ApplicationException(processException);
}
}
}
public abstract void process(FileInputStream input) throws IOException;
public static void main(String[] args) {
try {
new FileReadDemo() {
@Override
public void process(FileInputStream input) throws IOException {
byte[] buff = new byte[1024];
while (-1 != input.read(buff)) {
System.out.println(new String(buff));
}
}
}.readFile("D:/test/itart.txt");
} catch (ApplicationException e) {
e.printStackTrace();
}
}
}
最后分离模板, 整理效果如下:
模板类: FileInputStreamTemplate.java
public abstract class FileInputStreamTemplate {
public void readFile(String path) throws ApplicationException {
IOException processException = null;
FileInputStream input = null;
try {
input = new FileInputStream(new File(path));
process(input);
} catch (IOException e) {
processException = e;
} finally {
try {
if (null != input) {
input.close();
}
} catch (IOException e) {
if (null == processException) {
throw new ApplicationException(e);
} else {
throw new ApplicationException(
"FileInputStream close exception", processException);
}
}
if (processException != null) {
throw new ApplicationException(processException);
}
}
}
public abstract void process(FileInputStream input) throws IOException;
}
使用方式: Client.java
public abstract class Client {
public static void main(String[] args) throws ApplicationException {
new FileInputStreamTemplate() {
@Override
public void process(FileInputStream input) throws IOException {
byte[] buff = new byte[1024];
while (-1 != input.read(buff)) {
System.out.println(new String(buff));
}
}
}.readFile("D:/test/itart.txt");
}
}
这样代码是不是清晰很多, 为了让代码更加容易阅读, 我们可以进一步改进, 将模板方法改为静态模板方法, 定义一个process方法的接口, 具体如下
定义一个处理器接口InputStreamProcessor.java
public interface InputStreamProcessor {
public void process(FileInputStream input) throws IOException;
}
模板方法改为静态模板方法
public class FileInputStreamTemplate {
public static void readFile(String path, InputStreamProcessor processor) throws ApplicationException {
IOException processException = null;
FileInputStream input = null;
try {
input = new FileInputStream(new File(path));
processor.process(input);
} catch (IOException e) {
processException = e;
} finally {
try {
if (null != input) {
input.close();
}
} catch (IOException e) {
if (null == processException) {
throw new ApplicationException(e);
} else {
throw new ApplicationException(
"FileInputStream close exception", processException);
}
}
if (processException != null) {
throw new ApplicationException(processException);
}
}
}
}
调用方式: Client.java
public abstract class Client {
public static void main(String[] args) throws ApplicationException {
FileInputStreamTemplate.readFile("D:/test/itart.txt", new InputStreamProcessor() {
@Override
public void process(FileInputStream input) throws IOException {
byte[] buff = new byte[1024];
while (-1 != input.read(buff)) {
System.out.println(new String(buff));
}
}
});
}
}
InputStreamProcessor
的实现类是采用匿名实现方式, 这是因为一般情况下这个类是不会被重复使用, 没有必要再定义一个子类.
模板方法可以有效的减少重复代码, 提高代码的可阅读性, 可维护性, 实际项目开发中, 可以应用模板方法的地方很多, 就像IO操作的地方, 如: 读写文件, 还有JDBC, 事务管理等等