处理程序吞下异常
Handlers Swallow Exceptions
考虑以下处理程序:
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!");
}
}
从客户和开发人员的角度来看,执行此处理程序时会发生什么情况非常清楚:应该弹出某种错误消息。
发生的事情是:没有。
更准确:异常记录到错误日志(和控制台,如果从 Eclipse 启动)。但是用户什么也看不到,事实上他甚至不知道有错误。
我可以通过为每个处理程序捕获 Exception
来解决这个问题,但除了丑陋和笨重之外,它还与每个样式指南相矛盾。
是否有更好的方法来处理处理程序吞噬的异常?
对于 Eclipse 4(e4 或 3.x 兼容模式)添加 class 实现 IEventLoopAdvisor
到应用程序上下文。对于未处理的异常,将调用 eventLoopException
方法。
为 e4 设置它的合适位置是 RCP 生命周期的 @PostContextCreate
class:
@PostContextCreate
public void postContextCreate(IEclipseContext context)
{
// Event loop advisor for error handling
context.set(IEventLoopAdvisor.class, new EventLoopAdvisor());
你还必须实现eventLoopIdle
,这个调用display.sleep()
非常重要。标准方法是:
@Override
public void eventLoopIdle(final Display display)
{
display.sleep();
}
对于 3.x 兼容模式,在 post 上下文创建之后安装了一个默认事件循环顾问,它委托给 workbench WorkbenchAdvisor
。如果您在 RCP 中使用自己的顾问,您可以覆盖顾问的 eventLoopException
方法。
我找到了另一种适用于我的 E3 兼容性应用程序的方法:覆盖 WorkbenchAdvisor#eventLoopException(Throwable)
:
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {
@Override
public void eventLoopException(Throwable exception) {
// do magic here
}
// [snipped other methods]
}
考虑以下处理程序:
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!");
}
}
从客户和开发人员的角度来看,执行此处理程序时会发生什么情况非常清楚:应该弹出某种错误消息。
发生的事情是:没有。
更准确:异常记录到错误日志(和控制台,如果从 Eclipse 启动)。但是用户什么也看不到,事实上他甚至不知道有错误。
我可以通过为每个处理程序捕获 Exception
来解决这个问题,但除了丑陋和笨重之外,它还与每个样式指南相矛盾。
是否有更好的方法来处理处理程序吞噬的异常?
对于 Eclipse 4(e4 或 3.x 兼容模式)添加 class 实现 IEventLoopAdvisor
到应用程序上下文。对于未处理的异常,将调用 eventLoopException
方法。
为 e4 设置它的合适位置是 RCP 生命周期的 @PostContextCreate
class:
@PostContextCreate
public void postContextCreate(IEclipseContext context)
{
// Event loop advisor for error handling
context.set(IEventLoopAdvisor.class, new EventLoopAdvisor());
你还必须实现eventLoopIdle
,这个调用display.sleep()
非常重要。标准方法是:
@Override
public void eventLoopIdle(final Display display)
{
display.sleep();
}
对于 3.x 兼容模式,在 post 上下文创建之后安装了一个默认事件循环顾问,它委托给 workbench WorkbenchAdvisor
。如果您在 RCP 中使用自己的顾问,您可以覆盖顾问的 eventLoopException
方法。
我找到了另一种适用于我的 E3 兼容性应用程序的方法:覆盖 WorkbenchAdvisor#eventLoopException(Throwable)
:
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {
@Override
public void eventLoopException(Throwable exception) {
// do magic here
}
// [snipped other methods]
}