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/03 06:48] gthanos [Exception Handler] |
java:exceptions_intro [2015/03/03 07:02] gthanos [Exception Handler] |
||
---|---|---|---|
Line 127: | Line 127: | ||
while ((inputLine = in.readLine()) != null) { | while ((inputLine = in.readLine()) != null) { | ||
strDocument.append(inputLine); | strDocument.append(inputLine); | ||
+ | throw new IOException(); | ||
} | } | ||
+ | System.out.println("Closing File!"); | ||
fReader.close(); | fReader.close(); | ||
return strDocument.toString(); | return strDocument.toString(); | ||
Line 136: | Line 138: | ||
} | } | ||
catch(IOException ex) { | catch(IOException ex) { | ||
- | System.out.println("IOException occured while opening file or reading from file "+path); | + | System.out.println("IOException occured while reading from file "+path); |
- | System.out.println("Exiting..."); | + | |
- | System.exit(1); | + | |
} | } | ||
- | return ""; | + | return "Nothing to return.."; |
} | } | ||
| | ||
Line 153: | Line 153: | ||
} | } | ||
- | }</code> | + | } |
+ | </code> | ||
Παρατηρήστε ότι εμφανίζονται δύο **catch** blocks. Το πρώτο catch block αφορά [[http://docs.oracle.com/javase/7/docs/api/java/io/IOException.html|ΙΟException]] objects, ενώ το δεύτερο catch block αφορά [[http://docs.oracle.com/javase/7/docs/api/java/io/FileNotFoundException.html|FileNotFoundException]] objects. Από τους συνδέσμους που παρατίθενται θα παρατηρήσετε ότι το ''FileNotFoundException'' είναι υποκλάση του ''IOException''. Ο παραπάνω κώδικας θα μπορούσε να παραλείπει τις γραμμές | Παρατηρήστε ότι εμφανίζονται δύο **catch** blocks. Το πρώτο catch block αφορά [[http://docs.oracle.com/javase/7/docs/api/java/io/IOException.html|ΙΟException]] objects, ενώ το δεύτερο catch block αφορά [[http://docs.oracle.com/javase/7/docs/api/java/io/FileNotFoundException.html|FileNotFoundException]] objects. Από τους συνδέσμους που παρατίθενται θα παρατηρήσετε ότι το ''FileNotFoundException'' είναι υποκλάση του ''IOException''. Ο παραπάνω κώδικας θα μπορούσε να παραλείπει τις γραμμές | ||
Line 170: | Line 171: | ||
catch(IOException ex) { | catch(IOException ex) { | ||
System.out.println("IOException occured while opening file or reading from file "+path); | System.out.println("IOException occured while opening file or reading from file "+path); | ||
- | System.out.println("Exiting..."); | ||
- | System.exit(1); | ||
} | } | ||
catch(FileNotFoundException ex) { | catch(FileNotFoundException ex) { |