Thread: setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh)
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainClass extends JFrame {
public static void main(String[] args) {
new MainClass().setVisible(true);
}
public MainClass() {
Container cp = getContentPane();
JButton crasher = new JButton("Crash");
cp.add(crasher);
crasher.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
throw new RuntimeException("You asked for it");
}
});
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable ex) {
System.out.println("You crashed thread " + t.getName());
System.out.println("Exception was: " + ex.toString());
}
});
pack();
}
}
Related examples in the same category