Advertisement
yo2man

Tutorial 35 (Multiple Exceptions)

May 19th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. //Tutorial 35 (Multiple Exceptions)
  2. //App.java
  3.  
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.text.ParseException;
  7.  
  8. public class App {
  9.  
  10.     public static void main(String[] args) {
  11.         Test test = new Test();
  12.        
  13.         /*
  14.         // Multiple catch blocks
  15.         try {
  16.             test.run();
  17.         } catch (IOException e) {
  18.            
  19.         } catch (ParseException e) {
  20.             System.out.println("Couldn't parse command file.");
  21.         }
  22.         */
  23.        
  24.         /*
  25.         //Try Multi-catch (Java 7+ only)
  26.         try {
  27.             test.run();
  28.         } catch (IOException | ParseException e) {    //this is the Try Multi Catch. It can handle multiple exceptions
  29.             // TODO Auto-generated catch block
  30.             e.printStackTrace();
  31.         }
  32.         */
  33.         //Using polymorphisim to catch the parent of all exceptions (ie: IOException, ParseException or any child classes of exception)
  34.         try {
  35.             test.run();
  36.         } catch (Exception e) { //all exceptions derive from the parent class exception: "Exception" //thus just like we're passing methods to a parameter, we could catch any exception by using a parent class here "catch (parent-class-Exception  e)"
  37.             // TODO Auto-generated catch block
  38.             e.printStackTrace();
  39.    
  40.         }
  41.          // Important to catch exceptions in the right order!
  42.         // IOException cannot come first, because it's the parent
  43.         // of FileNotFoundException, so would catch both exceptions
  44.         // in this case.
  45.         try {
  46.             test.input();
  47.         } catch (FileNotFoundException e) {
  48.             // TODO Auto-generated catch block
  49.             e.printStackTrace();
  50.         } catch (IOException e) {
  51.             // TODO Auto-generated catch block
  52.             e.printStackTrace();
  53.         }
  54.     }
  55.  
  56. }
  57.  
  58.  
  59.  
  60. //---------------------------------------------------------------------------------------------------------------------------------------
  61. //Test.java
  62. import java.io.FileNotFoundException;
  63. import java.io.IOException;
  64. import java.text.ParseException;
  65.  
  66.  
  67. public class Test {
  68.     public void run() throws IOException, ParseException {
  69.        
  70.         //throw new IOException();
  71.        
  72.         throw new ParseException("Error in command list.", 2);
  73.     }
  74.     public void input() throws IOException, FileNotFoundException {
  75.        
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement