在 Google Guava EventBus 中引发异常
Bring Up Exceptions in Google Guava EventBus
Google Guava EventBus 吞下异常并记录它们。
我写了一个非常简单的应用程序来解释我的方法:
public class SimplePrinterEvent {
@Subscribe
public void doPrint(String s) {
int a = 2/0; //This is to fire an exception
System.out.println("printing : " + s );
}
}
演示
public class SimplePrinterEventDemo {
public static void main(String[] args) {
EventBus eventBus = new EventBus();
eventBus.register(new SimplePrinterEvent());
try{
eventBus.post("This is going to print");
}
catch(Exception e){
System.out.println("Error Occured!");
}
}
}
这永远不会到达 catch 块!
所以我添加了一个 SubscriberExceptionHandler 并覆盖了 handleException()。
EventBus eventBus = new EventBus(new SubscriberExceptionHandler() {
@Override
public void handleException(Throwable exception,
SubscriberExceptionContext context) {
System.out.println("Handling Error..yes I can do something here..");
throw new RuntimeException(exception);
}
});
它允许我在处理程序内部处理异常,但我的要求是将该异常带到顶层,我在那里处理它们。
编辑:我在某些网站上找到的旧解决方案。 (这适用于番石榴 v18)
public class CustomEventBus extends EventBus {
@Override
void dispatch(Object event, EventSubscriber wrapper) {
try {
wrapper.handleEvent(event);
} catch (InvocationTargetException cause) {
Throwables.propagate(Throwables.getRootCause(cause));
}
}
}
以下技巧对我有用:
最新的 EventBus class 有一个名为 handleSubscriberException()
的方法,您需要在扩展的 EventBus class 中重写该方法:
(这里我包括了两种解决方案,只有一种适用于您的版本)
public class CustomEventBus extends EventBus {
//If version 18 or bellow
@Override
void dispatch(Object event, EventSubscriber wrapper) {
try {
wrapper.handleEvent(event);
} catch (InvocationTargetException cause) {
Throwables.propagate(Throwables.getRootCause(cause));
}
}
//If version 19
@Override
public void handleSubscriberException(Throwable e, SubscriberExceptionContext context) {
Throwables.propagate(Throwables.getRootCause(e));
}
}
Google Guava EventBus 吞下异常并记录它们。
我写了一个非常简单的应用程序来解释我的方法:
public class SimplePrinterEvent {
@Subscribe
public void doPrint(String s) {
int a = 2/0; //This is to fire an exception
System.out.println("printing : " + s );
}
}
演示
public class SimplePrinterEventDemo {
public static void main(String[] args) {
EventBus eventBus = new EventBus();
eventBus.register(new SimplePrinterEvent());
try{
eventBus.post("This is going to print");
}
catch(Exception e){
System.out.println("Error Occured!");
}
}
}
这永远不会到达 catch 块!
所以我添加了一个 SubscriberExceptionHandler 并覆盖了 handleException()。
EventBus eventBus = new EventBus(new SubscriberExceptionHandler() {
@Override
public void handleException(Throwable exception,
SubscriberExceptionContext context) {
System.out.println("Handling Error..yes I can do something here..");
throw new RuntimeException(exception);
}
});
它允许我在处理程序内部处理异常,但我的要求是将该异常带到顶层,我在那里处理它们。
编辑:我在某些网站上找到的旧解决方案。 (这适用于番石榴 v18)
public class CustomEventBus extends EventBus {
@Override
void dispatch(Object event, EventSubscriber wrapper) {
try {
wrapper.handleEvent(event);
} catch (InvocationTargetException cause) {
Throwables.propagate(Throwables.getRootCause(cause));
}
}
}
以下技巧对我有用:
最新的 EventBus class 有一个名为 handleSubscriberException()
的方法,您需要在扩展的 EventBus class 中重写该方法:
(这里我包括了两种解决方案,只有一种适用于您的版本)
public class CustomEventBus extends EventBus {
//If version 18 or bellow
@Override
void dispatch(Object event, EventSubscriber wrapper) {
try {
wrapper.handleEvent(event);
} catch (InvocationTargetException cause) {
Throwables.propagate(Throwables.getRootCause(cause));
}
}
//If version 19
@Override
public void handleSubscriberException(Throwable e, SubscriberExceptionContext context) {
Throwables.propagate(Throwables.getRootCause(e));
}
}