Java Reference
In-Depth Information
8. char data = (char) fis.read();
9. System.out.println(“Just read: “ + data);
10. } catch(FileNotFoundException e) {
11. System.out.println(“Oops - file not found: “ +
12. e.getMessage());
13. } catch(IOException e) {
14. System.out.println(“Something went wrong”);
15. e.printStackTrace();
16. }
17. System.out.println(“End of readFromFile”);
18. }
19.
20. public static void main(String [] args) {
21. MyFileReader reader = new MyFileReader();
22. reader.readFromFile(“mydata.txt”);
23. System.out.println(“End of main”);
24. }
25.}
Here is the fl ow of control of
main
when no fi le is found:
1.
Line 21 instantiates a new
MyFileReader
object and its
readFromFile
method is
invoked on line 22 with the filename
mydata.txt
.
2.
The
try
block is entered on line 5.
3.
The
FileReader
constructor invoked on line 6 throws a
java.io
.FileNotFoundException
.
4.
Flow of control jumps to the first
catch
block on line 10. The
FileNotFoundException
is caught and
e
refers to it. Line 11 displays a message.
5.
The
catch
on line 13 is skipped because the exception has already been caught. Line 17
executes and the
readFromFile
method completes its execution.
6.
Control jumps to line 23.
End of main
displays and the program finishes successfully.
The output of running
MyFileReader
is
Oops - file not found: mydata.txt (The system cannot find the file specified)
End of readFromFile
End of main
Search WWH ::
Custom Search