Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Exemple des Exeptions Avancées


{filelink=4345}

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 TestException extends JFrame implements ActionListener {
    private double[] a;
  private JRadioButton boutonDivisionParZero;
 
  private JRadioButton boutonMauvaisCast;
 
  private JRadioButton boutonLimiteTableau;
 
  private JRadioButton boutonNullPointer;
 
  private JRadioButton boutonRacineCarNag;
 
  private JRadioButton boutonDebordement;
 
  private JRadioButton boutonFichierInconnu;
 
  private JRadioButton boutonExceptionInconnu;
 
  public TestException() {
    JPanel panel = new JPanel();
    ButtonGroup g = new ButtonGroup();
    panel.setLayout(new GridLayout(8, 1));
    boutonDivisionParZero = AjoutBoutonsRadio("Division Par zero", g, panel);
    boutonMauvaisCast = AjoutBoutonsRadio("Mauvaise Conversion", g, panel);
    boutonLimiteTableau = AjoutBoutonsRadio("Limite du Tableau", g, panel);
    boutonNullPointer = AjoutBoutonsRadio("Null pointer", g, panel);
    boutonRacineCarNag = AjoutBoutonsRadio("Racine Carrée Négative", g, panel);
    boutonDebordement = AjoutBoutonsRadio("Débordement", g, panel);
    boutonFichierInconnu = AjoutBoutonsRadio("Fichier Inexistant", g, panel);
    boutonExceptionInconnu = AjoutBoutonsRadio("Exception inconnu", g, panel);
    getContentPane().add(panel);
  }
 
  private JRadioButton AjoutBoutonsRadio(String s, ButtonGroup g, JPanel p) {
    JRadioButton button = new JRadioButton(s, false);
    button.addActionListener(this);
    g.add(button);
    p.add(button);
    return button;
  }
 
  public void actionPerformed(ActionEvent evt) {
    try {
      Object source = evt.getSource();
      if (source == boutonDivisionParZero) {
        a[1] = a[1] / a[1] - a[1];
      } else if (source == boutonMauvaisCast) {
        Frame f = (Frame) evt.getSource();
      } else if (source == boutonLimiteTableau) {
        a[1] = a[10];
      } else if (source == boutonNullPointer) {
        Frame f = null;
        f.setSize(200, 200);
      } else if (source == boutonRacineCarNag) {
        a[1] = Math.sqrt(-5);
      } else if (source == boutonDebordement) {
        a[1] = 1000 * 1000 * 1000 * 1000;
        int n = (int) a[1];
      } else if (source == boutonFichierInconnu) {
        FileInputStream is = new FileInputStream("Java Source and Support");
      } else if (source == boutonExceptionInconnu) {
        throw new UnknownError();
      }
    } catch (RuntimeException e) {
      System.out.println("Caught RuntimeException: " + e);
    } catch (Exception e) {
      System.out.println("Caught Exception: " + e);
    }
  }
 
  public static void main(String[] args) {
    JFrame frame = new TestException();
    frame.setSize(150, 200);
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    frame.show();
  }
}

A Voir sur le même Sujet:

  • Exception