Title: Simple Example
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Simple Example

1. Simple Example - This example shows a simple usage of Exceptions. All of the Exceptions in the following example are Derived from RuntimeException. This implies that handling these Exceptions is optional and not forced by the compiler. Compile this program and try the following executions of this program and explain the outcome:
  1. java SimpleExample 0 9
  2. java SimpleExample 0 0
  3. java SimpleExample
  4. java SimpleExample 11 1
  5. java SimpleExample 0 a


  class SimpleExample {
  static int compute(int a, int b) {
      int c;
      try
      {
        // Of b == 0, we will get an ArithmeticException
        c= a/b;
      }
      catch ( ArithmeticException e)
      {
         System.out.println(" Probably a divide by 0 case");
         c=0;
      }
      return c;
  }
  public static void main(String[] args) {
    int [] arr = new int[10];

    // Exceptions occur if we don't have enough parameters
    // We also get an Exception from parseInt if it a NON number
    int index = Integer.parseInt(args[0]);
    int i = Integer.parseInt(args[1]);

    int c = compute(10, i);

    // We can get an exception if index >= 10
    arr[index] = c;

    System.out.println(" Finished Simple Example");
  }
}


2. FixedExample - Now try the same executions as above except that the new program is called FixedExample instead of SimpleExample

  class FixedExample {
  static int compute(int a, int b) {
      int c;
     try
     {
       // An exception occurs if b == 0
       c= a/b;
     }
     catch ( ArithmeticException e)
     {
        System.out.println(" Probably a divide by 0 case");
        c=0;
     }
     return c;
  }
  public static void main(String[] args) {
    int [] arr = new int[10];

   try
      {
      // An exception will occur if we don't pass parameters
      // to this program, or the data passed is not something other
      // than a number.

       int index = Integer.parseInt(args[0]);

       int i = Integer.parseInt(args[1]);
       int c = compute(10, i);

       // if index >= 10 we get an exception
       arr[index] = c;
       }
   catch (NumberFormatException e)
       {
           System.out.println(" A non-numeric input parameter");
           e.printStackTrace();
       }
   catch (ArrayIndexOutOfBoundsException e)
       {
           System.out.println(" Array Index must be < 10");
           e.printStackTrace();
       }

   System.out.println(" Finished Simple Example");
  }
}


3. Note the use of printStackTrace. This useful function is expecially helpful in cases where the Exception can come from many different places
4. There are many styles for handling Exceptions. One last solution demonstrates handling all errors at the calling routine (main). It also demonstrates that catch clauses are processed in order until an Exception class match occurs. However, you can have a more generic class that catches all possibilities not already enumerated. The more generic class should be at the end of the list. The java compiler will give you a compilation errror if you get this backwards (i.e. more generic class before the more specific class). See the following example:

 class FixedExample2 {
 static void assign(int [] arr, int index, int a, int b)
  // The following line is optional because we are dealing
  // with RuntimeExceptions
 throws ArithmeticException, ArrayIndexOutOfBoundsException
  {
     // "ducking all Exceptions and passing it off to the caller
     arr[index] = a/b;
  }
  public static void main(String[] args) {
    int [] arr = new int[10];
    int index;

    try
       {
        index = Integer.parseInt(args[0]);
        int i = Integer.parseInt(args[1]);
        assign(arr, index, 10, i);
        }
    catch (NumberFormatException e)
        {
            System.out.println(" A non-numeric input parameter");
        }
    // The following will catch all Exceptions derived from RuntimeException that
    // haven't already been matched (i.e. NumberFormatException)
    catch (RuntimeException e)
        {
            System.out.println(" Some Runtime Exception: " +e);
            e.printStackTrace();
        }

    System.out.println(" Finished Simple Example");
  }
}


Last Updated: July 27, 2013