Demonstration de recupération d’une Exception non gérée

Author:


{filelink=4351}

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class ThreadBasedCatcher extends JFrame{

  public static void main(String[] args) {
    new ThreadBasedCatcher().setVisible(true);
  }
  public ThreadBasedCatcher(){
    Container cp = getContentPane();
    JButton crasher = new JButton("Crash");
    cp.add(crasher);
    crasher.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
        throw new RuntimeException("Exception");
      }
    });
    Thread.setDefaultUncaughtExceptionHandler(
        new Thread.UncaughtExceptionHandler(){
          public void uncaughtException(Thread t, Throwable ex){
            System.out.println(
              "Crash dans le thread " + t.getName());
            System.out.println(
              "Exception: " + ex.toString());
          }
        });
    pack();
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *