//NOTE THE FOLLOWING: // try{} must always be followed with catch{} and/or finally{} // finally is a statement that is always executed - whether an exception is handled or if the program crashes // ArithmeticException will occur on line 26 and line 44 // line 26: The program recovers from an ArithmeticException due to the try/catch handler // line 51 : An exception (ArithmeticException) can also be caught by its parent (RuntimeException) or grandparent (Exception) classes // line 80 : An exception (FileNotFoundException) can also be caught by its parent (IOException) or grandparent (Exception) classes // e.getMessage() prints the type of exception to the console screen // e.printStackTrace() prints the sequence of methods that were executed from start of the program untill the occurence of the exception import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.BufferedReader; import java.io.FileReader; public class ExceptionHandling2 { public static void main(String args[]){ System.out.println ("Starting Main Method"); m1(); m2(); m3(); System.out.println ("Ending Main Method"); } public static void m1(){ System.out.println("Start of method m1"); int x = 10; int y = 0; double z = 0; try{ z = x/y; // line 32 System.out.println ("The answer, z = " + z); } catch(ArithmeticException e){ System.out.println ("e.getMessage = " + e.getMessage()); e.printStackTrace(); } finally{ System.out.println ("Printing finally -m1"); } System.out.println("End of method m1" + "\n"); } public static void m2(){ System.out.println("Start of method m2"); int x = 10; int y = 0; double z = 0; try{ z = x/y; // line 51 System.out.println ("The answer, z = " + z); } catch(ArithmeticException e){ System.out.println ("e.getMessage = " + e.getMessage()); e.printStackTrace(); } catch(RuntimeException e){ // line 58 System.out.println ("e.getMessage = " + e.getMessage()); e.printStackTrace(); } catch(Exception e){ // line 62 System.out.println ("e.getMessage = " + e.getMessage()); e.printStackTrace(); } finally{ System.out.println ("Printing finally -m2"); } System.out.println("End of method m2" + "\n"); } public static void m3(){ System.out.println("Start of method m3"); Scanner scan = new Scanner (System.in); System.out.println ("Input file name to read"); String name = scan.nextLine(); File file = new File(name); try{ FileReader reader = new FileReader (file); // Line 80 BufferedReader br = new BufferedReader(reader); String st = ""; while (st != null){ st = br.readLine(); if (st != null){ System.out.println (st); } } } catch(FileNotFoundException e){ System.out.println ("e.getMessage = " + e.getMessage()); e.printStackTrace(); } catch(IOException e){ // line 92 System.out.println ("e.getMessage = " + e.getMessage()); e.printStackTrace(); } catch(Exception e){ // line 96 System.out.println ("e.getMessage = " + e.getMessage()); e.printStackTrace(); } finally{ System.out.println ("Printing finally -m3"); } System.out.println("End of method m3" + "\n"); } }