Eclipse plugin 处理程序接受异常
考虑以下处理程序:Eclipse plugin 处理程序接受异常,eclipse-plugin,Eclipse Plugin,考虑以下处理程序: 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 IllegalAr
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启动,则记录到控制台)。但是用户什么也看不到,事实上他甚至不知道有错误
我可以通过为每个处理程序捕获异常来解决这个问题,但除了丑陋和麻烦之外,它还与每个样式指南相矛盾
有没有更好的方法来处理处理程序吞没的异常?对于Eclipse4(e4或3.x兼容模式),在应用程序上下文中添加一个实现IEventLoopAdvisor的类。将为未处理的异常调用eventLoopException
方法
为e4设置此项的合适位置是RCP生命周期类的@PostContextCreate
:
@PostContextCreate
public void postContextCreate(IEclipseContext上下文)
{
//用于错误处理的事件循环顾问
set(IEventLoopAdvisor.class,新的EventLoopAdvisor());
您还必须实现eventLoopIdle
,调用display.sleep()
@覆盖
公共void eventLoopIdle(最终显示)
{
display.sleep();
}
对于3.x兼容模式,在创建post上下文之后安装了一个默认的事件循环顾问,它将委托给工作台WorkbenchHadVisor
。如果您在RCP中使用自己的顾问,您可以覆盖顾问的eventLoopException
方法。我找到了另一种适用于E3兼容的方法应用程序:重写WorkbenchHadVisor#eventLoopException(可丢弃)
:
我尝试使用添加LifeCycleManager
。调用了@PostContextCreate
,但没有调用IEventLoopAdvisor
的任何方法。你知道为什么吗?看起来3.x Compatibility mode会在稍后的启动过程中安装自己的事件循环顾问,并将其委派给WorkbenchHadvisor
-请参阅过时的回答。
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {
@Override
public void eventLoopException(Throwable exception) {
// do magic here
}
// [snipped other methods]
}