MODULE 6q - Exceptions


THE TRY-CATCH CONSTRUCT

Three different exceptions are referred to in the program below.  They
are the  ArrayIndexOutOfBoundsException  which is built-into Java and
two others, BadLuckException and BadZeroException, which are declared
(as classes) in the program.

public class TryCatch
 { private static int[] a = {12,22,0,19,13,29};
   private static int p=0;

   public static void main(String[] args)
    { while (true)
       { try
          { System.out.println(read());
          }
         catch(ArrayIndexOutOfBoundsException e)              // catch 1
	  { System.out.println("Finished");
            break;
          }
         catch(BadLuckException e)                            // catch 2
	  { System.out.println("---> " + e.getMessage());
            continue;
          }
       }
    }

   private static int read() throws ArrayIndexOutOfBoundsException,
                                    BadLuckException
    { int n=0;
      try
       { n = a[p++];
         if (n==0)
            throw new BadZeroException();
         if (n==13)
            throw new BadLuckException();
       }
      catch(BadZeroException e)                               // catch 3
       { System.out.print("---> ");
       }
      return n;
    }
 }

class BadLuckException extends Exception
 { public String getMessage()
    { return "unlucky thirteen";
    }
 }

class BadZeroException extends Exception
 { public String getMessage()
    { return "Zero";
    }
 }

// This yields:
//
// 12
// 22
// ---> 0
// 19
// ---> unlucky thirteen
// 29
// Finished


TRY IT OUT

Key the program in, compile it and run it.  It ought to give the results
which appear as comments at the end of the program.

Next see what happens if, in turn, the  catch 1  clause is omitted, the
catch 2  clause is omitted and the  catch 3  clause heading is changed to

         catch(BadLuckException e)

Note also what happens if the throws clause is, in turn, changed to

         throws ArrayIndexOutOfBoundsException
and to
         throws BadLuckException


THE TRY-CATCH-FINALLY CONSTRUCT

An adapted version of the above program is given here.  It refers to the
same three exceptions but includes a try-catch-finally construct.


public class TryFinally
 { private static int[] a = {12,22,0,19,13,29};
   private static int p=0;
   private static int totup=0;

   public static void main(String[] args)
    { while (true)
       { try
          { System.out.println(read());
          }
         catch(ArrayIndexOutOfBoundsException e)
	  { System.out.println("Finished");
            break;
          }
         catch(BadLuckException e)
	  { System.out.println("---> " + e.getMessage());
            continue;
          }
       }
      System.out.println("Read entered " + totup + " times");
    }

   private static int read() throws ArrayIndexOutOfBoundsException,
                                    BadLuckException
    { int n=0;
      try
       { n = a[p++];
         if (n==0)
            throw new BadZeroException();
         if (n==13)
            throw new BadLuckException();
       }
      catch(BadZeroException e)
       { System.out.print("---> ");
       }
      finally
       { totup++;
       }
      return n;
    }
 }

class BadLuckException extends Exception
 { public String getMessage()
    { return "unlucky thirteen";
    }
 }

class BadZeroException extends Exception
 { public String getMessage()
    { return "Zero";
    }
 }

// This yields:
//
// 12
// 22
// ---> 0
// 19
// ---> unlucky thirteen
// 29
// Finished
// Read entered 7 times


TRY IT OUT

Edit the previous program so that is appears as this new version,
compile it and run it.  It ought to give the results shown.


TRY THIS JIFFY PROGRAM TOO

public class HelloC
 { public static void main(String[] args) throws InterruptedException
    { System.out.println("Hello");
      Thread.sleep(3000L);
      System.out.println("World");
    }
 }