处理程序吞下异常 [英] Handlers Swallow Exceptions

查看:62
本文介绍了处理程序吞下异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下处理程序:

public class CreateProjectHandler extends AbstractHandler {

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        // it does not matter what kind of exception this is:
        throw new IllegalArgumentException("This is a test!");
    }

}

从客户和开发人员的角度来看,很清楚执行此处理程序时会发生什么:应该弹出某种错误消息。

From a customer and developer perspective it's pretty clear what should happen when this handler is executed: an error message of some kind should pop up.

发生了什么:什么都没有。

What happens is: Nothing.

更准确:将异常记录到错误日志中(如果是从Eclipse启动,则记录在控制台中)。但是用户看不到任何东西,实际上他甚至都不知道有错误。

More accurate: the exception is logged into the error log (and console, if started from Eclipse). But the user sees nothing, in fact he doesn't even know there was an error.

我可以通过捕获 Exception 每个处理程序,但是除了丑陋和麻烦之外,还与每个样式指南相抵触。

I could fix this by catching Exception for each and every handler, but besides being ugly and cumbersome it contradicts each style guide ever.

是否有更好的方法来处理处理程序吞下的异常?

Is there a better way to handle the exceptions swallowed by handlers?

推荐答案

对于Eclipse 4(e4或3.x兼容模式),添加一个实现 IEventLoopAdvisor 到应用程序上下文。对于未处理的异常,将调用 eventLoopException 方法。

For Eclipse 4 (e4 or 3.x compatbility mode) add a class implementing IEventLoopAdvisor to the application context. The eventLoopException method will be called for the unhandled exceptions.

将其设置为e4的合适位置是RCP生命周期类的 @PostContextCreate

A suitable place to set this up for e4 is the @PostContextCreate of the RCP life cycle class:

@PostContextCreate
public void postContextCreate(IEclipseContext context)
{
  // Event loop advisor for error handling

  context.set(IEventLoopAdvisor.class, new EventLoopAdvisor());

还必须实现 eventLoopIdle 调用 display.sleep()非常重要。一个标准方法是:

You must also implement eventLoopIdle, it is very important that this calls display.sleep(). A standard method would be:

@Override
public void eventLoopIdle(final Display display)
{
  display.sleep();
}

对于3.x兼容模式,在发布上下文,然后将其委派给工作台 WorkbenchAdvisor 。如果您在RCP中使用自己的顾问程序,则可以覆盖顾问程序的 eventLoopException 方法。

For 3.x compatibility mode there is a default event loop advisor installed after the post context create which delegates to the workbench WorkbenchAdvisor. If you are using your own advisor in the RCP you can override the eventLoopException method of the advisor.

这篇关于处理程序吞下异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆