// handle exceptions where/when they occur public class except3 { public static void main(String args[]) { int a = 1; int b = 0; // Beware of possbile exceptions thrown by a/b: // catch exceptions at the level of main, not inside test! try { test(a,b); } catch (ArithmeticException exception) { exception.printStackTrace(); // report why there was an exception System.out.println("You are dividing by zero, fool!"); } // catch caught the exception thrown by try block // so control returns here: System.out.println("Aren't you glad I accounted for that problem?"); } // method main public static void test(int a, int b) { System.out.println(a/b); // can't divide by zero } // method test } // class except2 /* Output: java.lang.ArithmeticException: / by zero at except3.test(Compiled Code) at except3.main(Compiled Code) You are dividing by zero, fool! Aren't you glad I accounted for that problem? */