Rotate your phone or change to desktop for better experience

Rotate your phone or change to desktop for better experience

Pgm 2 || Section 2 || Program to demonstrate exception handling with try, catch and finally.

 import java.util.*;

public class Pgm2

{

public static void main(String[] args)

{

Scanner in = new Scanner(System.in);

int a=0,b=0;

try

{

System.out.print("Enter a Number 1: ");

a = in.nextInt();


System.out.print("Enter a Number 2: ");

b = in.nextInt();

}

catch(NumberFormatException e)

{

System.out.println("You Caught Error in Input and Output");

}

try

{

int sum=a/b;

System.out.println("The Answer="+sum);

}

catch(ArithmeticException e)

{

System.out.println("You cannot divide any number by zero");

}

finally

{

System.out.println("Reached End of Program");

}

}

}

Post a Comment

0 Comments