Wednesday, December 14, 2016

Write a JAVA program that describes exception handling mechanism.

Write a JAVA program that describes exception handling mechanism.

import java.util.Scanner;
class DivisionByZero
{
  public static void main(String[] args)
  {

  int a, b, result;

  Scanner input = new Scanner(System.in);
  System.out.println("Input two integers");

  a = input.nextInt();
  b = input.nextInt();

  // try block

  try
  {
    result  = a / b;
    System.out.println("Result = " + result);
  }

  // catch block

  catch (ArithmeticException e)
  {
    System.out.println("Exception caught: Division by zero.");
  }
  }
}

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home