in reply to Re: Advantages of OO-ish exception handling..
in thread Advantages of OO-ish exception handling..
Note in this code, I don't have to worry about the error until the higher level calls in the main function, because all the functions that get called return exceptions that ISA FileException. If, however, readLine returned a different exception, I'd either have to make sure readDataFromFile also was declared to throw the same (or a parent of that) exception, OR add a specific try/catch block into that function as to remove that exception before it moves up the stack.class FileException inherits Exception { ... }; class BadFormatException inherits FileException { ... }; class IOException inherits Exception { ... }; // main code... // try { readDataFromFile( "file.dat" ); } catch ( FileException e ) { System.out.println("Something went wrong"); } int readDataFromFile( String filename ) throws FileException { // ... fileRef = openFile( filename ); while ( data = readLine( fileRef ) ) { ... } } File openFile( String filename ) throws IOException { ... } String readLine( File file ) throws BadFormatException { ... }
-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com
||
"You've left the lens cap of your mind on again, Pinky" - The Brain
"I can see my house from here!"
It's not what you know, but knowing how to find it if you don't know that's important
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Advantages of OO-ish exception handling..
by impossiblerobot (Deacon) on Dec 22, 2001 at 09:57 UTC | |
by Masem (Monsignor) on Dec 22, 2001 at 18:35 UTC |