Java Exception handle template
2018-06-16 本文已影响14人
熬夜的猫头鹰
Java Exception handle template
如果我们想要准确是处理异常,不至于导致异常的丢失,往往要写很多的代码就行处理,例如:
Input input = null;
IOException processException = null;
try{
input = new FileInputStream(fileName);
//...process input stream...
} catch (IOException e) {
processException = e;
} finally {
if(input != null){
try {
input.close();
} catch(IOException e){
if(processException != null){
throw new MyException(processException, e,
"Error message..." +
fileName);
} else {
throw new MyException(e,
"Error closing InputStream for file " +
fileName;
}
}
}
if(processException != null){
throw new MyException(processException,
"Error processing InputStream for file " +
fileName;
}
上面的代码是不是很难阅读,每调用一次读写人间的代码就会写一遍如上的异常处理的代码,简直就是噩梦。不过幸好我们有设计模式,我们可以使用模板模式进行处理
public abstract class InputStreamProcessingTemplate {
public void process(String fileName){
IOException processException = null;
InputStream input = null;
try{
input = new FileInputStream(fileName);
doProcess(input);
} catch (IOException e) {
processException = e;
} finally {
if(input != null){
try {
input.close();
} catch(IOException e){
if(processException != null){
throw new MyException(processException, e,
"Error message..." +
fileName);
} else {
throw new MyException(e,
"Error closing InputStream for file " +
fileName;
}
}
}
if(processException != null){
throw new MyException(processException,
"Error processing InputStream for file " +
fileName;
}
}
public abstract void doProcess(InputStream input) throws IOException;
}
我们只需要实现抽象方法 doProcess,然后调用process方法即可。
new InputStreamProcessingTemplate(){
public void doProcess(InputStream input) throws IOException{
int inChar = input.read();
while(inChar !- -1){
//do something with the chars...
}
}
}.process("someFile.txt");
是不是干净清爽。
我们也可以使用接口的方式替代继承
public interface InputStreamProcessor {
public void process(InputStream input) throws IOException;
}
public class InputStreamProcessingTemplate {
public void process(String fileName, InputStreamProcessor processor){
IOException processException = null;
InputStream input = null;
try{
input = new FileInputStream(fileName);
processor.process(input);
} catch (IOException e) {
processException = e;
} finally {
if(input != null){
try {
input.close();
} catch(IOException e){
if(processException != null){
throw new MyException(processException, e,
"Error message..." +
fileName;
} else {
throw new MyException(e,
"Error closing InputStream for file " +
fileName);
}
}
}
if(processException != null){
throw new MyException(processException,
"Error processing InputStream for file " +
fileName;
}
}
}
调用的时候:
new InputStreamProcessingTemplate()
.process("someFile.txt", new InputStreamProcessor(){
public void process(InputStream input) throws IOException{
int inChar = input.read();
while(inChar !- -1){
//do something with the chars...
}
}
});
也可以使用静态方法处理;
public class InputStreamProcessingTemplate {
public static void process(String fileName,
InputStreamProcessor processor){
IOException processException = null;
InputStream input = null;
try{
input = new FileInputStream(fileName);
processor.process(input);
} catch (IOException e) {
processException = e;
} finally {
if(input != null){
try {
input.close();
} catch(IOException e){
if(processException != null){
throw new MyException(processException, e,
"Error message..." +
fileName);
} else {
throw new MyException(e,
"Error closing InputStream for file " +
fileName;
}
}
}
if(processException != null){
throw new MyException(processException,
"Error processing InputStream for file " +
fileName;
}
}
}
InputStreamProcessingTemplate.process("someFile.txt",
new InputStreamProcessor(){
public void process(InputStream input) throws IOException{
int inChar = input.read();
while(inChar !- -1){
//do something with the chars...
}
}
});
总结:
异常处理模板是一个简单而强大的机制,可以提高代码的质量和可读性。 它也提高了你的生产力,因为你写的代码要少得多,而不用担心。 异常由模板处理。 而且,如果您稍后在开发过程中需要改进异常处理,则只能在单个位置更改它:异常处理模板。
模板方法设计模式可以用于除异常处理之外的其他目的。 输入流的迭代也可以被放入模板中。 JDBC中的ResultSet的迭代可以放在一个模板中。 JDBC中的事务的正确执行可以放在一个模板中。 可能性是无止境的。