Source of TestExceptionHandling.java


  1: //TestExceptionHandling.java
  2: 
  3: import java.util.Scanner;
  4: import java.util.Random;
  5: 
  6: public class TestExceptionHandling
  7: {
  8:     public static void main(String args[])
  9:     {
 10:         //Exception handling
 11:         //==================
 12:         //System.out.println(6/0);
 13:         
 14:         try
 15:         {
 16:             System.out.println(6/0);
 17:         }
 18:         catch (ArithmeticException e)
 19:         {
 20:             System.out.println(e.getMessage());
 21:         }
 22: 
 23:         /*
 24:         //Illustrating try-throw-catch
 25:         Scanner keyboard = new Scanner(System.in);
 26:         boolean finished = false;
 27:         do
 28:         {
 29:             try
 30:             {
 31:                 System.out.print("\nEnter an integer: ");
 32:                 String input = keyboard.nextLine();
 33:                 int inputAsInt = Integer.parseInt(input);
 34:                 System.out.println("The value of 6 / " + inputAsInt
 35:                     + " is " + (6 / inputAsInt) + ".");
 36:                 System.out.print("Enter another value to divide? [y/[n]] ");
 37:                 String response = keyboard.nextLine();
 38:                 if (!response.equalsIgnoreCase("y")) finished = true;
 39:             }
 40:             catch (ArithmeticException e)
 41:             {
 42:                 System.out.println(e.getMessage() + "\nCan't do that!"
 43:                     + "\nTry again.");
 44:             }
 45:             catch (NumberFormatException e)
 46:             {
 47:                 System.out.println("The number you entered was not an "
 48:                     + "integer.\nTry again.");
 49:             }
 50:         }
 51:         while (!finished);
 52:         */
 53:     }
 54: }
 55: