Apache NetBeans
Apache NetBeans
Latest release

Apache NetBeans 23

Download

How can I customize the Unexpected Exception dialog ?

Problem: How do I add new functionality to the Unexpected Exception dialog?

Solution: First, one needs to create a new class as follows.

The first part of this class is that it must extend Handler:

public class NewFunctionExceptionHandler extends Handler {


    @Override
    public void publish(LogRecord record) {
        if (record.getThrown() != null) {
            // This is an uncaught exception being thrown.
        }
    }

    @Override
    public void flush() {
    }

    @Override
    public void close() throws SecurityException {
    }
}

In order to properly process the exceptions, one must do a check for the exception in the publish() method; `LogRecord`s created due to exceptions being thrown will always have `Throwable`s present.

This class also must implement Callable<JButton>. The button we want displayed in the Uncaught Exception dialog needs to be returned in the call() method:

public class NewFunctionExceptionHandler extends Handler implements Callable<JButton> {


    @Override
    public void publish(LogRecord record) {
        if (record.getThrown() != null) {
            // This is an uncaught exception being thrown.
        }
    }

    @Override
    public void flush() {
    }

    @Override
    public void close() throws SecurityException {
    }

    // Return the button we want to be displayed in the Uncaught Exception Dialog.
    @Override
    public JButton call() throws Exception {
        ...
    }
}

The JButton’s action listener needs to be passed the LogRecord that passed via the publish() method. Then, within said action listener for the button, the developer can do what is needed with that record (e.g. Open a Top Component to email a bug report or do anything else).

The final result will look similar to:

public class NewFunctionExceptionHandler extends Handler implements Callable<JButton> {

    private JButton newFunctionButton;
    private NewFunctionActionListener newFunctionActionListener = new NewFunctionActionListener();

    @Override
    public void publish(LogRecord record) {
        if (record.getThrown() != null) {
            newFunctionActionListener.setLogRecord(record);
        }
    }

    @Override
    public void flush() {
    }

    @Override
    public void close() throws SecurityException {
    }

    // Return the button we want to be displayed in the Uncaught Exception Dialog.
    @Override
    public JButton call() throws Exception {
        if (newFunctionButton == null) {
            newFunctionButton = new JButton("Review and Submit Issue");
            newFunctionButton.addActionListener(newFunctionActionListener);
        }

        return reviewIssueButton;
    }

    private class NewFunctionActionListener implements ActionListener {

        private LogRecord logRecord;

        public NewFunctionActionListener() {
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // Close our Uncaught Exception Dialog first.
            SwingUtilities.windowForComponent(reviewIssueButton).setVisible(false);
        }

        public void setLogRecord(LogRecord logRecord) {
            this.logRecord = logRecord;
        }
    }
}

To register this exception handler, one only needs to add the new Handler to a java.util.Logger named with the empty string:

   Logger.getLogger("").addHandler(new NewFunctionExceptionHandler());

Any Handler attached to the "" Logger that also implements Callable<JButton> will have its button displayed in the Uncaught Exception Dialog.

This could be done in a module’s Installer class.

Applies to: NetBeans IDE 6.0 and newer

Platforms: All