Looking at a question I got wrong on a midterm test, this has me wondering:
public double[] readInputFile(String fielane) throws IOException
{
File inputFile = new File(filename);
Scanner in = new Scanner(inputFile);
try
{
readData(in);
return data;
}
finally
{
inputFile.close();
}
}
Will this pass all exceptions back up the chain, or will it only pass the checked IOException?
The
finallyblock will be executed, and then originally-thrown exception (that is, the first exception that was thrown as a result ofreadData(in)) will percolate up.I suppose that the caveat in this question is about the type of exception that will be percolated up. That could either be an
IOException, or any sort of unchecked exception (that is, subclasses ofjava.lang.RuntimeExceptionorjava.lang.Error).EDITED as per @zapl’s comment: if the
finallyblock throws anIOException(as a result ofinputFile.close()), that exception will be percolated to the caller, regardless of whether thetryblock threw an exception or not.