Source of TestExceptions.java


  1: //TestExceptions.java
  2: //Do some questionable things just to see what exception is thrown ...
  3: 
  4: import java.util.Scanner;
  5: 
  6: public class TestExceptions
  7: {
  8:     public static void main(String[] args)
  9:     {
 10:         try
 11:         {
 12:             //Activate the lines below one at at time
 13:             //System.out.println(5/0); //ArithmeticException
 14:             //System.out.println(args[5]); //ArrayIndexOutOfBoundsException
 15:             System.out.println(Math.sqrt(-1)); //Actually we get NaN
 16:         }
 17:         catch (ArithmeticException e)
 18:         {
 19:             System.out.println("Sorry ... can't do that!");
 20:         }
 21:         catch (ArrayIndexOutOfBoundsException e)
 22:         {
 23:             System.out.println("Sorry ... can't do that either!");
 24:         }
 25:         catch (Exception e)
 26:         {
 27:             System.out.println("Who knows what happened ... ?");
 28:         }
 29:     }
 30: }
 31: 
 32: /*
 33:  * Note that the exceptions above are runtime exceptions, and as such are
 34:  * unchecked and do not need to be explicitly dealt with by the programmer.
 35:  */
 36: