NOTE

1.1 如何优雅的处理异常

1. 不优雅的处理方法 业务逻辑代码包裹在异常处理中,完全看不出实际的logic 2. 优雅的处理方法 2.1. 模板方法 - 模板类 - how to use 2.2. 接口 - 接口 //业务处理类作为一个接口 - how to use it

Java创建于 更新于 historical

这是历史学习笔记,可能存在过时或不完整的理解。

1. 不优雅的处理方法

业务逻辑代码包裹在异常处理中,完全看不出实际的logic

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;
}

2. 优雅的处理方法

2.1. 模板方法

  • 模板类
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;
    }
}

//override this method in a subclass, to process the stream.
public abstract void doProcess(InputStream input) throws IOException;
}
  • how to use
new InputStreamProcessingTemplate(){
					//只需要重写doProcess方法即可
    public void doProcess(InputStream input) throws IOException{
        int inChar = input.read();
        while(inChar !- -1){
            //do something with the chars...
        }
    }
}.process("someFile.txt");

2.2. 接口

  • 接口 //业务处理类作为一个接口
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;
    }
}
}
  • how to use it
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...
            }
        }
    });