Source of DivideByZeroNoExceptionHandling.java


  1: // Fig. 13.1: DivideByZeroNoExceptionHandling.java
  2: // An application that attempts to divide by zero.
  3: import java.util.Scanner;
  4: 
  5: public class DivideByZeroNoExceptionHandling
  6: {
  7:    // demonstrates throwing an exception when a divide-by-zero occurs
  8:    public static int quotient( int numerator, int denominator )
  9:    {
 10:       return numerator / denominator; // possible division by zero
 11:    } // end method quotient
 12: 
 13:    public static void main( String args[] )
 14:    {
 15:       Scanner scanner = new Scanner( System.in ); // scanner for input
 16: 
 17:       System.out.print( "Please enter an integer numerator: " );
 18:       int numerator = scanner.nextInt();
 19:       System.out.print( "Please enter an integer denominator: " );
 20:       int denominator = scanner.nextInt();
 21: 
 22:       int result = quotient( numerator, denominator );
 23:       System.out.printf( 
 24:          "\nResult: %d / %d = %d\n", numerator, denominator, result );
 25:    } // end main
 26: } // end class DivideByZeroNoExceptionHandling
 27: 
 28: 
 29: /**************************************************************************
 30:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 31:  * Pearson Education, Inc. All Rights Reserved.                           *
 32:  *                                                                        *
 33:  * DISCLAIMER: The authors and publisher of this book have used their     *
 34:  * best efforts in preparing the book. These efforts include the          *
 35:  * development, research, and testing of the theories and programs        *
 36:  * to determine their effectiveness. The authors and publisher make       *
 37:  * no warranty of any kind, expressed or implied, with regard to these    *
 38:  * programs or to the documentation contained in these books. The authors *
 39:  * and publisher shall not be liable in any event for incidental or       *
 40:  * consequential damages in connection with, or arising out of, the       *
 41:  * furnishing, performance, or use of these programs.                     *
 42:  *************************************************************************/