Custom Exception Reporting in a NetBeans RCP application

Recently, on the NetBeans mailing list, it was asked how to implement a custom exception reporter on an application based on the NetBeans platform. Some years ago I had to do the same thing and I want to share my experience.

I made a sample project on GitHub for reference, but the process itself is very easy.

In my sample project, I created a suite with two modules, one that generates an exception and one that registers itself as the Error Manager and handles the exceptions of the application.

I called the former module Exception Reporter and it just contains one class called MyErrorManager. To implement it quickly I just copied the original one, made it compile, replaced the old one and added the code to handle the exceptions in my own way.

The original NetBeans implementation is org.netbeans.core.NbErrorManager and it is located in o.n.core/src/org/netbeans/core/NbErrorManager.java inside the NetBeans sources, when I copied it I renamed MyErrorManager;
if you reuse it as well please keep the license in account please.

Then I added some dependencies to make it compile:

  • Lookup API: org.openide.util.lookup
  • Startup: org.netbeans.core.startup
  • Utilities API: org.openide.util.ui

The Startup module requires more attentions, I needed to access to some classes that are not public and when I tried to compile, it complained that my module is not a friend of the Startup module. To make it work I selected the Startup module, pressed Edit…, checked Implementation Version and pressed OK. This made almost compile my module compile.

Then I scrolled to about line 97 and replaced this line:

NotifyExcPanel.notify(ex);

with my custom code


EventQueue.invokeLater(() -> {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.t.printStackTrace(pw);
pw.flush();

String message = "Your Exception:\n" + sw.toString();
JOptionPane.showMessageDialog(null, message);
});

This just shows a popup with the Exception stack trace, but it can be customized to make something more useful like connecting to the company bug tracker or asking the user what he was doing.

Finally, I needed to remove the default NetBeans Error Manager, I could do this by setting the supersedes property to the annotation that registers the error manager.

@org.openide.util.lookup.ServiceProvider(service = java.util.logging.Handler.class, supersedes = {"org.netbeans.core.NbErrorManager"})

Now when I have an Exception my custom Exception Manager will be called.

The image was taken from here.

 

Leave a Comment

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