/**

 * This example shows how to handle the exception caused by

 * trying to divide by 0 - It is an ArithmeticException

 *

 * @author David Bernstein  modified by Elizabeth Adams

 * Date: September 1, 2008

*/

 

   public class Example1_Key

   {

      public static void main(String[] args)

      {

         int     denominator, numerator, ratio;

     

         numerator   = 5;

         denominator = 0;

     

         try    // to perform the division

         {

            ratio = numerator / denominator;

            System.out.println("The answer is: "+ ratio);

        

         } // end try

 

            // the expected exception is an ArithmeticException which is a class

            // the ArithmeticException object could have had any name

            // I prefer letters corresponding to the class name.

         catch (ArithmeticException ae)

         {

            System.out.println ("Attempt to divide by 0 ");

 

                // the ArithmeticException object calls its method

            ae.printStackTrace();

          

            System.out.println("Done."); // Don't move this line

         } // end catch

 

      } // end main

 

   } // end class