1/5/08

New Improved!!

Odi fetches me up about exception handling.

Instead of this:

finally
{
try
{
if (fos != null)
{
fos.close();
}

if (osw != null)
{
osw.close();
}

if (theFile != null)
{
theFile.close();
}
}
catch (IOException ignored)
{
log4jLogger.error(ignored);
}
}
Do this:
finally
{
close(fos);
close(osw);
close(theFile);
}

private void close(Closeable closeMe)
{
if (closeMe != null)
{
try
{
closeMe.close();
}
catch(IOException ignore)
{
log4jLogger.error(ignore);
}
}
}
That way, if an exception is thrown early on, your code will continue trying to close the other resources.

No comments: