On 8/28/2013 9:22 AM, Jeremy Isnard wrote:
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.


First of all, excellent experimentation.

The following is my explanation of what you've observed

- array size gets determined when an array is created not when an array is declared In other words, "private double[ ] a" just declares the type of array but not create one. So there is no a [0], a[1], yet. And if you try to access it, either you will experience ArrayOutOfBounds exception of NullPointexception.

- "double" is a bit funny animal. Dividing a number by double type like "3/0.0" results in NaN (meaning Not a number) not ArtithmeticException. Dividing a number by int like "3/0" results in ArtithmeticException.

So once you create an array with "private double[] = new double[9]", you no longer experience
NullPoint exception.

Having said this, I realized the example code is not really a good one. I am modifying the code
as following

public class Main extends JFrame implements ActionListener {
private int[] a = new int[10];
    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(8, 1));

        // 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) {
        try {
            Object source = evt.getSource();
            if (source == divideByZeroButton) {
      int x = 3 / 0;
            } else if (source == badCastButton) {
                Frame f = (Frame) evt.getSource();
            } else if (source == arrayBoundsButton) {
                a[1] = a[10];
            } else if (source == nullPointerButton) {
                Frame f = null;
                f.setSize(200, 200);
            } else if (source == negSqrtButton) {
double x = Math.sqrt(-1);
System.out.println("Is Math.sqrt(-1) result NaN? " + Double.isNaN(x));
            } else if (source == overflowButton) {
                a[1] = 1000 * 1000 * 1000 * 1000;
                int n = (int) a[1];
            } 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.setVisible(true);
    }
}



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.


--
-------------------------------------------------------------------
             Sang Shin, [email protected]
  Founder and Chief Instructor of JPassion.com (JavaPassion.com)
         http://www.linkedin.com/in/javapassion (Linkedin)
          http://twitter.com/javapassion (Tweeter)
            Life is worth living... with Passion!

   Practically Free 3 to 5 days Live, Hands-on, Online Codecamps on
 Java, HTML5, Ruby/Rails, Grails, JavaScript/jQuery, Spring, Android
             http://jpassion.com/codecamps
----------------------------------------------------------------------

--
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