Tuesday, 29 August 2017

how to Handle all exceptions that can be generated in this java program

Java program that divides two numbers. Handle all exceptions that can be generated in this program

Program

import java.util.Scanner;
class Division {
  public static void main(String[] args) {
  int a, b, Ans;
  Scanner input = new Scanner(System.in);
  System.out.println("Input First integers");
  a = input.nextInt();
  System.out.println("Input Second integers");
  b = input.nextInt();
            try {
                   Ans  = a / b;
                   System.out.println("Result = " + Ans);
            }
            catch (ArithmeticException e) {
                   System.out.println("Exception caught: Division by zero.");
            }

  }      }

Output

 Handle all exceptions java Core

0 comments:

Post a Comment