NOTE

How to Handle Exceptions Elegantly

1. An inelegant approach 2. An elegant approach

JavaCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. An Inelegant Approach

The business-logic code is wrapped in exception-handling code, making the actual logic difficult to see.

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. An Elegant Approach

2.1. Template Method

  • Template class
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 it
new InputStreamProcessingTemplate(){
                    // Only the doProcess method needs to be overridden.
    public void doProcess(InputStream input) throws IOException{
        int inChar = input.read();
        while(inChar !- -1){
            //do something with the chars...
        }
    }
}.process("someFile.txt");

2.2. Interface

  • Interface

Use an interface for the business-processing class.

public interface InputStreamProcessor {
    public void process(InputStream input) throws IOException;
}


// The exception-handling class accepts the business-processing class as a parameter.

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

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub