Java Nested Exception Handling -


i came across code base read text file , analyzes it. bit confused way exceptions used. separate class appfilereaderexception extends exceptions has been defined extended class returns error message exception. besides, function getsymbol() uses both throws , try , catch block. error() function has exception handler result in nested exceptions ! there advantage of doing such exception handling basic try , catch should sufficient? there reason extend exception class, combine both throws , try-catch block? these on kill or there reason have such constructs?

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

//------------------------------------------------------------------

the extended class appfilereaderexception follows:

package appname; public class appfilereaderexception extends exception {       public appfilereaderexception(string msg)      {         super(msg);     } } 

first, error() method (not function!) not have handling. throws exception given message.

creating own exception classes may useful when calling methods; can like

public void methodthatcallslibrary() {    try {       dosomething();       new appfilereader().getsymbol();       doothersomething();    } catch (appfilereaderexception afre) {      // handling specific appfilereader    } catch (exception e) {       // handling related rest of code.    } } 

that said, system here little odd. creating exception in error() method, stacktrace of exception same possible places exception raised. also, looks masks ioexception, go forwarding ioexception (and, if not, include nested exception in exception thrown, give better debugging information).


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -