
// DivideByZeroTest.java
// Andrew Davison, Jan 2018, ad@fivedots.coe.psu.ac.th

// Checking for a divide-by-zero-error.


import java.util.Scanner;


public class DivideByZeroTest
{
  public static void main(String args[])
  {
    Scanner s = new Scanner( System.in );
    System.out.print("Enter first float: ");
    float x = s.nextFloat();
    System.out.print("Enter second float: ");
    float y = s.nextFloat();
    s.close();

    float result;
    try {         
      result = quotient(x, y);
    }
    catch (DivideByZeroException e) {
      System.out.println("Attempted to Divide by Zero\n");
      result = Float.NaN;    // "Not a Number" constant
    }
    System.out.println("Division gives: " + result);

  }  // end of main()


  private static float quotient(float x, float y)
                  throws DivideByZeroException
  {
    if (y == 0)
      throw new DivideByZeroException();
    return x/y;
  }  // end of quotient()


}  // end of DivideByZeroTest class
