Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Exception Handling in an Eclipse RCP app

I want to override the global Exception Handling in my RCP app. Whenever an uncaught Exception happens I want to log it (using java logging) and then exit the app. I have already overwritten the eventLoopException(Throwable exception) method in the ApplicationWorkbenchAdvisor class. But this catches only the event loop exceptions. As of now I have also overwritten the postStartup() method like this:

public void postStartup()
{
    Policy.setStatusHandler(new StatusHandler()
    {
       @Override
          public void show(IStatus status, String title)
          {
            LOGGER.log(Level.SEVERE, "Uncaught Exception", status.getException());
            UnexpectedErrorDialog();
            PlatformUI.getWorkbench().close();
          }
    });
}

It logs the exception in my log file and exits the app. But it's obviously not right and the exception is shown twice in the console, cause all I do is intercepting the showing of the exception in a gui dialog to the user. So how can I properly overwrite/change the global exception handling, so that my code (log) is used instead of the default one?

like image 982
Biene Maja Avatar asked Nov 07 '12 11:11

Biene Maja


2 Answers

I would suggest you to use org.eclipse.ui.statusHandlers extension point

like image 148
sambi reddy Avatar answered Sep 20 '22 11:09

sambi reddy


Thanks to sambi reddy's tip i have now overwritten AbstractStatusHandler in the ApplicationWorkbenchAdvisor class

@Override
public synchronized AbstractStatusHandler getWorkbenchErrorHandler() {
    if (myStatusHandler == null) {
        myStatusHandler = new MyStatusHandler();
    }
    return myStatusHandler;
}

MyStatusHandler extends AbstractStatusHandler and i have overwritten the handle method like this:

@Override
public void handle(StatusAdapter statusAdapter, int style)
{
    if(statusAdapter.getStatus().matches(IStatus.ERROR) && ((style != StatusManager.NONE)))
    {
            LOGGER.log(Level.SEVERE, "Uncaught Exception", statusAdapter.getStatus().getException());
            UnexpectedErrorDialog();
            PlatformUI.getWorkbench().close();
    }
}

seems to work right, only downside is that i still get 2 console outputs.

like image 31
Biene Maja Avatar answered Sep 22 '22 11:09

Biene Maja