import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class ExTest extends JApplet
{
  public void init()
  {
    Container pane = getContentPane();
    pane.add(new ExTestPanel()); 
  }
} 

class ExTestPanel extends Box
{
  private ButtonGroup group;
  private JTextField textField;
  private double[] a = new double[10];

  public ExTestPanel()
  {
    super(BoxLayout.Y_AXIS);
    group = new ButtonGroup();

    addRadioButton("Integer divide by zero", new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        a[1] = 1 / (a.length - a.length);
      }
    });

    addRadioButton("Floating point divide by zero", new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        a[1] = a[2] / (a[3] - a[3]);
      }
    });

    addRadioButton("Array bounds", new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        a[1] = a[10];
      }
    });
 
    addRadioButton("Bad cast", new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        a = (double[]) e.getSource();
      }
    });

    addRadioButton("Null pointer", new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        e = null;
        System.out.println(e.getSource());
      }
    });

    addRadioButton("sqrt(-1)", new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        a[1] = Math.sqrt(-1);
      }
    });

    addRadioButton("Overflow", new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        a[1] = 1000 * 1000 * 1000 * 1000;
        int n = (int) a[1];
      }
    });

    addRadioButton("No such file", new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        try
        {
          InputStream in = new FileInputStream("junk.txt");
        }
        catch(IOException ee)
        {
          textField.setText(ee.toString());
        }
      }
    });

    textField = new JTextField(30);
    add(textField);
  }

  private void addRadioButton(String s, ActionListener listener)
  {
    JRadioButton button = new JRadioButton(s, false)
    {
      protected void fireActionPerformed(ActionEvent e)
      {
        try
        {
          textField.setText("No exception");
          super.fireActionPerformed(e);
        }
        catch(Exception ee)
        {
          textField.setText(ee.toString());
        }
      }
    };

    button.addActionListener(listener);
    add(button);
    group.add(button);
  }
}
