...
A compliant code example would release all acquired resources, regardless of any exceptions which might occur. Hence, in the compliant code below, even though bufRead might result in an exception, if a FileInputStream object was instantiated, it will be closed.
| Code Block | ||
|---|---|---|
| ||
FileInputStream stream = null;
BufferedReader bufRead = null;
String line;
try {
stream = new FileInputStream(fileName);
bufFread = new BufferedReader(stream);
while((line=bufRead.readLine())!=null) {
sendLine(line);
}
} catch (IOException e) { }
catch {FileNotFoundException e) { }
finally {
try {
if(bufRead != null) {
bufRead.close();
}
}
finally {
if(stream != null) {
stream.close();
}
}
}
|
...