title: Java Nested Exception Handling
body: <p>I came across a code base that read a text file and analyzes it. I am bit confused with the way the exceptions are used. A separate class <code>AppFileReaderException</code> that <code>extends</code> exceptions has been defined where the extended class only returns the error message for the exception. Besides, the function <code>getSymbol()</code> uses both <code>throws</code> and <code>try and catch block</code>. The <code>error()</code> function has an exception handler too which could result in nested exceptions ! Is there any advantage of doing such exception handling where the basic try and catch should be sufficient? Is there any reason to extend the exception class, combine both <code>throws</code> and <code>try-catch</code> block? Are these over kill or there is a good reason to have such constructs?</p>
  
  <pre><code>package AppName;    
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.IOException;
  import java.io.LineNumberReader;
  
  public class AppFileReader {
  
  
      // 
      public char getSymbol() throws AppFileReaderException {
          try {
  
          //do something
  
         } catch (Exception e) {
              error("IO Error: " + fileName + "@" + currentLineNumber);
          }
          return somechar;
      }
  
  
      public void error(String errorMsg) throws AppFileReaderException {
          throw new AppFileReaderException(errorMsg);
      }
  
      public AppFileReader(String fileName) throws FileNotFoundException {
          reader = new LineNumberReader(new FileReader(fileName));
          this.fileName = fileName;
      }
  
  }
  </code></pre>
  
  <p>//------------------------------------------------------------------</p>
  
  <p>The extended class for the <code>AppFileReaderException</code> is as follows:</p>
  
  <pre><code>package AppName;
  public class AppFileReaderException extends Exception {
  
  
      public AppFileReaderException(String msg) 
      {
          super(msg);
      }
  }
  </code></pre>