I don't want to pull out a Java book to demonstrate what I mean, but let's take this hypothetical case where I want to read a file that has a specific format. I can do the following (*code isn't correct, but the idea is there*) in Java:
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 { ... }
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.
-----------------------------------------------------
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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|