java:exceptions_intro

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
java:exceptions_intro [2015/03/02 16:45]
gthanos [Finaly Block]
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);​ 
 +        } 
 +      } 
 +    } 
 +    
   }   }
   ​   ​
java/exceptions_intro.txt · Last modified: 2019/04/20 05:02 by gthanos