Integer division without exception handling. - Java Object Oriented Design

Java examples for Object Oriented Design:Exception

Description

Integer division without exception handling.

Demo Code

import java.util.Scanner;

public class Main
{
   // demonstrates throwing an exception when a divide-by-zero occurs
   public static int quotient(int numerator, int denominator)
   {/*www  .  j av a  2s  .  c  o  m*/
      return numerator / denominator; // possible division by zero
   }

   public static void main(String[] args)
   {
      Scanner scanner = new Scanner(System.in); 

      System.out.print("Please enter an integer numerator: ");
      int numerator = scanner.nextInt();
      int denominator = 0;

      int result = quotient(numerator, denominator);
      System.out.printf("%nResult: %d / %d = %d%n", numerator, denominator, result);
   }
}

Related Tutorials