Calling close()
method on FIleInputStream
, has to be surrounded by try catch
clause. The API method itself is throwing the IOException exception. Also IOException is the checked exception, so we need to handle this.
Checked Exception
Checked exceptions are checked at compile-time. It means if a method
is throwing a checked exception then it should handle the exception
using try-catch block or it should declare the exception using throws
keyword, otherwise the program will give a compilation error. It is
named as checked exception because these exceptions are checked at
Compile time.
From the API,
/**
* Closes this file input stream and releases any system resources
* associated with the stream.
*
* <p> If this stream has an associated channel then the channel is closed
* as well.
*
* @exception IOException if an I/O error occurs.
*
* @revised 1.4
* @spec JSR-51
*/
public void close() throws IOException {
}
Since the close()
, method is throwing IOException
, either you need to re-throw the same
or surround the statement with try/catch
.
Refer this
Also talking about Java7, conveniently you can handle the same
1.) Catching Multiple Exception
2.) The try-with-resources Statement
To understand the causes of IOException, please refer similar question here.