Hi, 

This exercise should throw different exceptions through a box containing 
Radio buttons that trigger exceptions of different kinds ; fact is that for 
most of them -those using the double[] a declared- only a 
NullPointerException is thrown. 

I assumed it is because each array item is null as they are never defined. 

I tried to modify the code to trigger the proper exceptions, so I started 
to fill the array "a" through a loop (basically a[i]=i). 

Problem is that I do not get any exceptions thrown for the Math.sqrt(-1), 
the dividing by zero nor for the overflow... and I do not understand why. 

I tried to move by loop around (maybe as it is placed at the beginning of 
actionPerformed() it overrides the assigned values of the following 
multiple if() section?) but it does not seem to work any better. 

Any help ? 

*Code : * 

import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileInputStream;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class Main extends JFrame implements ActionListener {
    
    *private double[] a = new double[9];*

    private JRadioButton divideByZeroButton;
    
    private JRadioButton badCastButton;
    
    private JRadioButton arrayBoundsButton;
    
    private JRadioButton nullPointerButton;
    
    private JRadioButton negSqrtButton;
    
    private JRadioButton overflowButton;
    
    private JRadioButton noSuchFileButton;
    
    private JRadioButton throwUnknownButton;
    
    public Main() {
        
        // Create a JPanel and GridLayout
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(4, 2));
        
        // Create buttons and add them to the panel
        ButtonGroup g = new ButtonGroup();
        divideByZeroButton = addRadioButton("Divide by zero", g, p);
        badCastButton = addRadioButton("Bad cast", g, p);
        arrayBoundsButton = addRadioButton("Array bounds", g, p);
        nullPointerButton = addRadioButton("Null pointer", g, p);
        negSqrtButton = addRadioButton("sqrt(-1)", g, p);
        overflowButton = addRadioButton("Overflow", g, p);
        noSuchFileButton = addRadioButton("No such file", g, p);
        throwUnknownButton = addRadioButton("Throw unknown", g, p);
        getContentPane().add(p);
    }
    
    private JRadioButton addRadioButton(String s, ButtonGroup g, JPanel p) {
        JRadioButton button = new JRadioButton(s, false);
        button.addActionListener(this);
        g.add(button);
        p.add(button);
        return button;
    }

    
    // Trigger and catch various exceptions
    public void actionPerformed(ActionEvent evt) {
       
      * for (int i=0; i<9; i++){
            a[i]=i; 
        }*
            
        try {
            Object source = evt.getSource();
            if (source == divideByZeroButton) {
                a[1] = a[1]/(a[1]-a[1]); 
            } else if (source == badCastButton) {
                Frame f = (Frame) evt.getSource();
            } else if (source == arrayBoundsButton) {
                a[2] = a[10];
            } else if (source == nullPointerButton) {
                Frame f = null;
                f.setSize(200, 200);
            } else if (source == negSqrtButton) {
                a[3] = Math.sqrt(-1);
            } else if (source == overflowButton) {
                a[4] = 1000 * 1000 * 1000 * 1000 * 1000;
                int n = (int) a[4];
            } else if (source == noSuchFileButton) {
                FileInputStream is = new FileInputStream("Java Source and 
Support");
            } else if (source == throwUnknownButton) {
                throw new UnknownError();
            }
        } catch (RuntimeException e) {
            System.out.println("I Caught RuntimeException myself: " + e);
        } catch (Exception e) {
            System.out.println("I Caught Exception myself: " + e);
        }
    }
    
    public static void main(String[] args) {
        JFrame frame = new Main();
        frame.setSize(150, 200); 
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        }); 
        frame.pack(); 
        frame.setVisible(true);
    }
}
 

-- 
You received this message because you are subscribed to the Google Groups 
"JPassion.com: Java Programming" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
Visit this group at http://groups.google.com/group/jpassion_java.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to