public class ExceptionHandling1 { public static void method1(){ System.out.println ("Start of method1"); int x = 10; int y = 0; double z = 0; z = x/y; System.out.println ("z = " + z); System.out.println ("End of method1" + "\n"); } public static void method2(){ System.out.println ("Start of method2"); int x = 10; int y = 0; double z = 0; try{ z = x/y; System.out.println ("z = " + z); } catch(ArithmeticException e){ System.out.println ("You cannot divide an integer by 0!!"); System.out.println ("e.getMessage() = " + e.getMessage()); e.printStackTrace(); } System.out.println ("End of method2" + "\n"); } public static void method3(){ System.out.println ("Start of method3"); int a[] = {1,2,3}; try{ System.out.println(a[3]); System.out.println ("I will not be reached"); } catch(ArrayIndexOutOfBoundsException e){ System.out.println ("No such cell number in the array!!"); System.out.println ("e.getMessage() = " + e.getMessage()); e.printStackTrace(); } System.out.println ("End of method3" + "\n"); } public static void method4(){ System.out.println ("Start of method4"); String s = "hey"; try{ System.out.println(s.charAt(5)); System.out.println ("I will not be reached"); } catch(StringIndexOutOfBoundsException e){ System.out.println ("No such character in the String!!"); System.out.println ("e.getMessage() = " + e.getMessage()); e.printStackTrace(); } System.out.println ("End of method4" + "\n"); } public static void method5(){ System.out.println ("Start of method5"); String s = null; try{ System.out.println(s.length()); System.out.println ("I will not be reached"); } catch(NullPointerException e){ System.out.println ("The String object has a null reference!!"); System.out.println ("e.getMessage() = " + e.getMessage()); e.printStackTrace(); } System.out.println ("End of method5" + "\n"); } public static void main(String args[]){ // No Errors or Exceptions in main() System.out.println ("Starting Main Method"); //method1(); method2(); method3(); method4(); method5(); System.out.println ("Ending Main Method"); } }