20-CS-694 Advanced Programming Techniques Spring 2012
Exceptions

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

       Previous     Next         Applet Source     All lectures

Handle Exception

Acting on a mouse click - adding an exception to catch input error. The problem with the previous applet is that 1 cannot be added to a String object and will cause an exception if attempted. In the code below that exception is caught and handled gracefully (by setting the click count to 0).

Instructions: Same as previous applet. This time the exception is caught
and remedial action (set the count text field to 0) is taken.

import java.awt.*;         // for Panel, TextField, BorderLayout
import java.awt.event.*;   // for MouseMotionListener
import javax.swing.*;
import java.applet.*;

class MousePad extends JPanel implements MouseMotionListener {
   JTextField mouse_x, mouse_y, count;
   
   MouseAdapter ma = new MouseAdapter() {
      public void mousePressed (MouseEvent event) {
         try {
            count.setText(String.valueOf(Integer.parseInt(count.getText())+1));
         }
         catch (Exception e) {
            count.setText("0");
         }
      }
   };
   
   MousePad (JTextField mouse_x, JTextField mouse_y, JTextField count) {
      this.mouse_x = mouse_x;
      this.mouse_y = mouse_y;
      this.count = count;
      addMouseMotionListener(this);
      addMouseListener(ma);
      mouse_x.setEditable(false);               // cannot edit textfield
      mouse_x.setBackground(Color.white);       // set textfield color
      mouse_y.setEditable(false);
      mouse_y.setBackground(new Color(162, 195, 255));
   }
   
   public void mouseMoved (MouseEvent event) {
      mouse_x.setText(String.valueOf(event.getX()));
      mouse_y.setText(String.valueOf(event.getY()));
   }

   public void mouseDragged (MouseEvent event) { }
}

class InputExceptionFrame extends JFrame implements ActionListener {
   JTextField mouse_x, mouse_y, count;
   JButton reset_button;
   
   public InputExceptionFrame () {
      setLayout(new BorderLayout());
      JPanel p = new JPanel();
      p.setLayout(new GridLayout(1,6));
      p.add(new JLabel("x: ",JLabel.RIGHT));
      p.add(mouse_x = new JTextField());
      p.add(new JLabel("y: ",JLabel.RIGHT));      
      p.add(mouse_y = new JTextField());
      p.add(new JLabel("count:",JLabel.RIGHT));
      p.add(count = new JTextField("0"));
      add("North", p);
      
      MousePad mp = new MousePad(mouse_x, mouse_y, count);
      add("Center",mp);
      
      p = new JPanel();
      p.setLayout(new GridLayout(1,3));
      p.add(new JLabel(""));
      p.add(reset_button = new JButton("Reset"));
      p.add(new JLabel(""));
      add("South",p);
      
      reset_button.addActionListener(this);
   }
   
   public void actionPerformed (ActionEvent event) {
      if (event.getSource() == reset_button) {
         count.setText("0");
      }
   }
}

public class InputException extends Applet {
   public void init () {
      InputExceptionFrame df = new InputExceptionFrame();
      df.setSize(500,300);
      df.setVisible(true);
   }
}