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

public class ExTest2 
{
    public static void main(String[] args)
    {
	SwingApp app = new SwingApp(450, 270);	    // set app window size
	app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
    }
} 

class SwingApp extends JFrame
{
    public SwingApp(int width, int height)	
    {
	super();				    // call to super class constructor
	Container pane = super.getContentPane();    // create layout, set colors
	pane.add(new ExTestPanel()); 

	Toolkit toolkit = Toolkit.getDefaultToolkit();	// optionally position JFrame
	Dimension screenSize = toolkit.getScreenSize(); // in the middle of the screen 
	super.setLocation((screenSize.width - width)/2, (screenSize.height - height)/2);	
	super.setTitle("Swing app");		    // set desired window title
	super.setSize(width, height);		    // set desired window size
	super.setVisible(true);	                    // make the app window visible	    
    }
}

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);
    }
}

