Advertisement
yo2man

Tutorial 34 (Handling Exceptions)

May 18th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. //Tutorial 34 (Handling Exceptions)
  2. //Exception is part of an error handling mechanism
  3.  
  4.  
  5.  
  6. //---------------------------------------------------------------------------------------------------------------------------------------
  7. //demo1/App.java:
  8.  
  9. import java.io.File;
  10. import java.io.FileNotFoundException;
  11. import java.io.FileReader;
  12.  
  13.  
  14.  
  15. public class App {
  16.  
  17.     public static void main(String[] args) throws FileNotFoundException {
  18.        
  19.         File file = new File("test.txt");
  20.        
  21.         FileReader fr = new FileReader(file);
  22.     }
  23.  
  24. }
  25.  
  26.  
  27. /*when you run it you should get an error in red:
  28.  
  29. Exception in thread "main" java.io.FileNotFoundException: test.txt (The system cannot find the file specified)
  30.     at java.io.FileInputStream.open(Native Method)
  31.     at java.io.FileInputStream.<init>(Unknown Source)
  32.     at java.io.FileReader.<init>(Unknown Source)
  33.     at App.main(App.java:16)
  34. */
  35.  
  36.  
  37.  
  38.  
  39. //---------------------------------------------------------------------------------------------------------------------------------------
  40. //demo2/App.java:
  41.  
  42. import java.io.File;
  43. import java.io.FileNotFoundException;
  44. import java.io.FileReader;
  45.  
  46. public class App {
  47.  
  48.     public static void main(String[] args) {
  49.         File file = new File("test.txt");
  50.  
  51.         try {
  52.             FileReader fr = new FileReader(file);
  53.            
  54.             // This will not be executed if an exception is thrown.
  55.             System.out.println("Continuing ....");
  56.         } catch (FileNotFoundException e) {
  57.             System.out.println("File not found: " + file.toString());  //Remember to Right click>new>file>text.txt or else you will get an error: File Not Found test.txt
  58.         }
  59.  
  60.         System.out.println("Finished.");
  61.     }
  62.  
  63. }
  64.  
  65.  
  66.  
  67. /*run results:
  68. Continuing ....
  69. Finished.
  70. */
  71.  
  72.  
  73.  
  74.  
  75.  
  76. //---------------------------------------------------------------------------------------------------------------------------------------
  77. //demo3/App.java:
  78. package demo3;
  79.  
  80. import java.io.File;
  81. import java.io.FileNotFoundException;
  82. import java.io.FileReader;
  83.  
  84. public class App {
  85.  
  86.     public static void main(String[] args) {
  87.         try {
  88.             openFile();
  89.         } catch (FileNotFoundException e) {
  90.             System.out.println("Could not open file"); //if error, user will see "Could not open file"
  91.         }
  92.     }
  93.  
  94.     public static void openFile() throws FileNotFoundException {
  95.         File file = new File("test.txt");
  96.  
  97.         FileReader fr = new FileReader(file);
  98.  
  99.     }
  100.  
  101. }
  102.  
  103.  
  104. /*
  105. run results should be blank if successful
  106. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement