next up previous contents
Next: Java Database Connectivity (JDBC) Up: Introduction to UML and Previous: UML Examples   Contents

Exceptions [19]

Consider the following C-like code for handling error conditions:

if (function1())                 // boolean return type indicates success/failure
  if (function2())
    if (function3())
      print("all functions OK");
    else
      print("error function3");
  else
    print("error function2");
else 
  print("error function1");

Or this code:

function1(&error);              // call-by-reference error code indicates success/failure
if (!error) {
  function2(&error);
  if (!error) {
    function3(&error):
    if (!error)
      print("all functions OK");
  }
}
if (error)
  print("error: %d",error); 

Instead of cascading ``if'' statements, write code that assumes good behavior:

function1();
function2();
function3();
// handle any possible errors

$\bullet$ That is the point of Exception handling in Java

$\bullet$ Note that printing may not be a suitable method for ``handling'' error events

$\bullet$ A function may not be able to handle an event, and then must pass it along



Exceptions [20]


\begin{picture}(260,145)(220,655)\thicklines
\put(360,715){\framebox (120,25)...
...
\put(290,700){\line(-2, 3){ 10}}
\put(280,700){\line( 0,-1){ 20}}
\end{picture}





\begin{picture}(240,80)(80,720)\thicklines
\put(140,720){\framebox (120,80){}...
...){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm 1.1.1: c(i)}}}
\end{picture}

class MyException extends Exception {
  public MyException() { super(); }
  public MyException(String s) { super(s); }
}

class MyOtherException extends Exception {
  public MyOtherException() { super(); }
  public MyOtherException(String s) { super(s); }
}

class MySubException extends MyException {
  public MySubException() { super(); }
  public MySubException(String s) { super(s); }
}

public class Throwtest {
  public static void main(String argv[]) { 
    int i;
    try { i = Integer.parseInt(argv[0]); }
    catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("Must specify an argument");
      return;
    }
    catch (NumberFormatException e) {
      System.out.println("Must specify an integer argument");
      return;
    }
    a(i); 
  } 
}



Exceptions [21]

  public static void a(int i){
    try b(i); 
    catch (MyException e) {
      if (e instanceof MySubException)
        System.out.print("MySubException: "); 
      else
        System.out.print("MyException: "); 
      System.out.println(e.getMessage());
      System.out.println("Handled at point 1");
    }
  }
  
  public static void b(int i) throws MyException {
    int result;
    try {
      System.out.print("i="+i+" ");
      result = c(i);
      System.out.println("c(i)="+result);
    }
    catch (MyOtherException e) {
      System.out.println("MyOtherException: "+e.getMessage());
      System.out.println("Handled at point 2");
    }
  }
  
  public static int c(int i) throws MyException, MyOtherException {
    switch (i) {
      case 0 : // processing resumes at point 1 above
               throw new MyException("input too low"); 
      case 1 : // processing resumes at point 1 above
               throw new MySubException("input still too low");
      case 99: // input resumes at point 2 above
               throw new MyOtherException("input too high");
      default: return i*i;
    }
  }



Ted Billard 2006-09-26