of some operation. If the operation generated an exception / error, it is handled by the user defined exception class. The main method in the application class is declared to throw a user-defined exception which will take care of the exception. import // for input from keyboard public class MyExceptionApp { public static void main( ) throws MyDivByZeroException { Scanner kb = System.out.println("Enter two numbers for division : "); double n1 = kb.nextDouble(); double n2 = kb.nextDouble(); try{ if( n2 == 0 )
Complete the following Java
import // for input from keyboard public class MyExceptionApp { public static void main( ) throws MyDivByZeroException {
Scanner kb = System.out.println("Enter two numbers for division : "); double n1 = kb.nextDouble(); double n2 = kb.nextDouble();
try{ if( n2 == 0 ) throw else System.out.println("Answer = " + n1/n2); } catch ( e){ System.out.println("Exception caught : " + e); } System.out.println("Program terminated normally ... "); } } // definition of MyDivByZeroException class class MyDivByZeroException { { // default constructor is required } { // a constructor with String argument is also required System.out.println(msg); } } |
Step by step
Solved in 4 steps with 4 images