17/02/25 ---------- Q)Java Program that terminates abnormally if exception is raised but not handled. //WithoutHandling.java import java.util.Scanner; class WithoutHandling{ public static void main(String[] args) { int quotient=1;//identity of division System.out.println("Program started"); Scanner scanner=new Scanner(System.in); System.out.print("Enter the numerator:"); int n=scanner.nextInt(); System.out.print("Enter the denominator:"); int d=scanner.nextInt(); quotient=n/d; System.out.println("Quotient is "+quotient); System.out.println("Program ended successfully"); } } Q)Explain about "try" in the context of exception handling. =>one of the 5 keywords of eh m. =>we can create a block of code try{ }//try block =>"try" is one of the 5 keywords of Java exception handling mechanism. =>"try" keyword is used to create a block of code. =>risky code(that may cause exception) is placed in try block. =>If exception is raised in try block, instead of program getting terminated, try block gets terminated. Q)Explain about "catch" in the context of exception handling. =>"catch" is one of the 5 keywords of exception handling mechanism. =>"catch" keyword is used to create a block of code. =>exception handling code is written in catch block and therefore it is known as an exception handler. =>Between try and catch blocks no code is allowed. =>If exception is raised in the try block, control goes to catch block. =>If exception is not raised in the try block, control never goes to catch block. =>One try block can be associated with any number of catch blocks. Q)Modify the previous application(Withouthandling.java)so as to implement exception handling. //WithHandling.java import java.util.Scanner; class WithHandling{ public static void main(String[] args) { int quotient=1;//identity of division System.out.println("Program started"); Scanner scanner=new Scanner(System.in); System.out.print("Enter the numerator:"); int n=scanner.nextInt(); System.out.print("Enter the denominator:"); int d=scanner.nextInt(); try{ quotient=n/d;//risky code System.out.println("Quotient is "+quotient); }catch(ArithmeticException e){ System.out.println("Division failed. Please ensure that denominator is not zero"); }//exception handler System.out.println("Program ended successfully"); } }