20-CS-4003-001 Organization of Programming Languages Fall 2017
Exceptions

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures

Exception Handling

Java:

  import java.awt.*;
  import java.awt.event.*;
  import java.util.*;
  import javax.swing.*;
  import java.applet.*;

  interface Variable {
     void checkit() throws Up, Out;  String v();
  }

  class Up extends Exception { }

  class Out extends Exception { }

  class Puzzle {
     public String v() { return ""; }
     public void assert_ (boolean e) throws Up {
        if (!e) throw new Up();
     }
  }

  class Choose implements Variable {
     String [] strs;
     Variable variable;
     String val;
     int n;

     public Choose(String s, Variable var) {
        variable = var;
        StringTokenizer t =
           new StringTokenizer(s," ");
        n = t.countTokens();
        strs = new String[n];
        for (int i=0 ; i < n ; i++)
           strs[i] = t.nextToken();
     }

     public String v() { return val; }

     public void checkit () throws Up, Out {
        for (int i=0 ; i < n ; i++)  {
           val = strs[i];
           try {
              variable.checkit();
           } catch (Up up) { }
        }
        throw new Up();
     }
  }

  class Evaluate
     extends Puzzle implements Variable {
     Variable iv,jv,kv;
     ExceptionFrame f;

     public Evaluate (ExceptionFrame f) {
        this.f = f;
        f.out.append("\nSolve: ((1-i)*j"+
                     " + i*(1-k) + (1-j)*k)\n\n");
     }

     public void checkit() throws Up, Out {
        int i = Integer.parseInt(iv.v());
        int j = Integer.parseInt(jv.v());
        int k = Integer.parseInt(kv.v());

        assert_((((1-i)*j+i*(1-k)+(1-j)*k) % 2)==1);
        f.out.append("i="+i+" j="+j+" k="+k+"\n");

        throw new Out();
     }

     public void solve () {
        try {
           kv = new Choose("0 1", this);
           jv = new Choose("0 1", kv);
           iv = new Choose("0 1", jv);
           iv.checkit();
        } catch (Up up) {
           f.out.append("No solution\n");
        } catch (Out out) {  }
     }
  }

  class ExceptionFrame
     extends JFrame implements ActionListener {
     JTextArea out;
     JButton button;

     public ExceptionFrame () {
        setLayout(new FlowLayout());
        add(new JScrollPane(out = new JTextArea(16,30)));
        add(button = new JButton("Press Me"));
        button.addActionListener(this);
        out.setFont(new Font("TimesRoman",Font.PLAIN,18));
     }

     public void doit() {
        Evaluate eval = new Evaluate(this);
        eval.solve();
     }

     public void actionPerformed (ActionEvent evt) {
        doit();
     }
  }

  public class exception
     extends Applet implements ActionListener {
     JButton go;

     public void init () {
        setLayout(new BorderLayout());
        setBackground(new Color(255,255,223));
        add("Center", go = new JButton("Applet"));
        go.addActionListener(this);
     }

     public void actionPerformed (ActionEvent evt) {
        Exception_Frame hf = new Exception_Frame();
        hf.setSize(550,450);
        hf.setVisible(true);
     }
  }
 -  An exception is a special condition that interrupts the normal course of execution of a program such as divide-by-zero, buffer overflow, read-before-write, among others. Thus, exceptions can arise due to arithmetic anomalies, safety violations, escaping (long jumps into the system stack) and other phenomenon. Some languages allow users to define their own exceptions. The Java example to the left shows how exceptions can be defined and used to control execution, in this case to implement search.