// 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) { // report why there was an exception exception.printStackTrace(); System.out.println("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?"); } public static void test(int a, int b) { System.out.println(a/b); // can't divide by zero } } /* 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? */