This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Next revision Both sides next revision | ||
java:exceptions_intro [2015/03/02 16:46] gthanos |
java:exceptions_intro [2015/03/02 16:55] gthanos [Finaly Block] |
||
---|---|---|---|
Line 156: | Line 156: | ||
<code java> | <code java> | ||
- | |||
import java.io.*; | import java.io.*; | ||
import java.lang.*; | import java.lang.*; | ||
Line 163: | Line 162: | ||
| | ||
public String readFile(String path) { | public String readFile(String path) { | ||
+ | FileReader fReader = null; | ||
| | ||
try { | try { | ||
File file = new File (path); | File file = new File (path); | ||
- | FileReader fReader = new FileReader(file); | + | fReader = new FileReader(file); |
BufferedReader in = new BufferedReader(fReader); | BufferedReader in = new BufferedReader(fReader); | ||
String inputLine; | String inputLine; | ||
Line 180: | Line 180: | ||
System.out.println("Exiting..."); | System.out.println("Exiting..."); | ||
System.exit(1); | System.exit(1); | ||
- | } | + | } |
- | finally { | + | |
- | if( fReader != null) { | + | |
- | try { | + | |
- | //System.out.println("Closing file"); | + | |
- | fReader.close(); | + | |
- | } | + | |
- | catch(IOException ex) { | + | |
- | System.out.println("IOException occured while reading from file "+path); | + | |
- | System.out.println("Exiting..."); | + | |
- | System.exit(1); | + | |
- | } | + | |
- | } | + | |
- | } | + | |
return strDocument.toString(); | return strDocument.toString(); | ||
} | } | ||
Line 199: | Line 186: | ||
System.out.println("The specified file was not found at "+ path); | System.out.println("The specified file was not found at "+ path); | ||
return ""; | return ""; | ||
- | } | + | } |
+ | finally { | ||
+ | if( fReader != null) { | ||
+ | try { | ||
+ | System.out.println("Closing file"); | ||
+ | fReader.close(); | ||
+ | } | ||
+ | catch(IOException ex) { | ||
+ | System.out.println("IOException occured while reading from file "+path); | ||
+ | System.out.println("Exiting..."); | ||
+ | System.exit(1); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
} | } | ||
| |