Java Reference
In-Depth Information
ception; and finally you clean up from either the normal code path or
the exception code path, whichever actually happened.
Here is a getdataSet method that returns a set of data read from a file.
If the file for the data set cannot be found or if any other I/O exception
occurs, this method throws an exception describing the error. First, we
define a new exception type BadDataSetException to describe such an er-
ror. Then we declare that the method getdataSet tHRows that exception
using a throws clause in the method header:
class BadDataSetException extends Exception { }
class MyUtilities {
public double[] getDataSet(String setName)
throws BadDataSetException
{
String file = setName + ".dset";
FileInputStream in = null;
try {
in = new FileInputStream(file);
return readDataSet(in);
} catch (IOException e) {
throw new BadDataSetException();
} finally {
try {
if (in != null)
in.close();
} catch (IOException e) {
; // ignore: we either read the data OK
// or we're throwing
BadDataSetException
}
}
}
// ... definition of readDataSet ...
}
 
Search WWH ::




Custom Search