import java.util.Scanner; //ArithmeticException is in java.lang, so we need not import that. class TryException { // uncomment the next line, if we just want to claim the exception. Then get rid of the try, catch and finally blocks public static void main(String [] args) //throws ArithmeticException { //Everything in the try block is the code that could produce exceptions when stuff goes wrong try { int a=0,b=1; System.out.println("Enter 2 numbers :"); Scanner in = new Scanner (System.in); a = in.nextInt(); b = in.nextInt(); // uncomment the next line, if we just want to throw the exception. Then get rid of the try, catch and finally blocks /*if(b == 0) throw new ArithmeticException("You tried to divide by zero"); else*/ System.out.println("The result is " + (a/b)); } // This is the area that the code skips to, if the code in the try block produces an Arithmetic Exception catch(ArithmeticException e) { System.out.println(e.getMessage()); e.printStackTrace(); } //This code is executed all the time, even if the code doesn't produce an exception. finally{ System.out.println("Congratulations!! You know how to handle exceptions"); } } }