Java 番石榴事件巴士:don';无法捕获运行时异常

Java 番石榴事件巴士:don';无法捕获运行时异常,java,exception,guava,runtimeexception,Java,Exception,Guava,Runtimeexception,我使用guava的EventBus,不幸的是它捕获并记录了事件处理程序抛出RuntimeException时发生的InvocationTargetException。我可以禁用此行为吗?目前,这是一个经过深思熟虑的决定,在EventBus文档中讨论: 一般来说,处理程序不应该抛出。如果这样做,EventBus将捕获并记录异常。对于错误处理来说,这很少是正确的解决方案,因此不应依赖于此;它仅用于帮助在开发过程中发现问题 其他解决方案是,尽管我严重怀疑它们是否能进入第12版。以下是懒惰的代码 pub

我使用guava的EventBus,不幸的是它捕获并记录了事件处理程序抛出RuntimeException时发生的InvocationTargetException。我可以禁用此行为吗?

目前,这是一个经过深思熟虑的决定,在EventBus文档中讨论:

一般来说,处理程序不应该抛出。如果这样做,EventBus将捕获并记录异常。对于错误处理来说,这很少是正确的解决方案,因此不应依赖于此;它仅用于帮助在开发过程中发现问题

其他解决方案是,尽管我严重怀疑它们是否能进入第12版。

以下是懒惰的代码

public class Events
{
    public static EventBus createWithExceptionDispatch()
    {
        final EventBus bus;

        MySubscriberExceptionHandler exceptionHandler = new MySubscriberExceptionHandler();
        bus = new EventBus(exceptionHandler);
        exceptionHandler.setBus(bus);
        return bus;
    }

    private static class MySubscriberExceptionHandler implements SubscriberExceptionHandler
    {
        @Setter
        EventBus bus;

        @Override
        public void handleException(Throwable exception, SubscriberExceptionContext context)
        {
            ExceptionEvent event = new ExceptionEvent(exception, context);
            bus.post(event);
        }
    }
}
现在,您可以订阅
ExceptionEvent

这是我的
例外事件
,仅用于复制和粘贴

@Data
@Accessors(chain = true)
public class ExceptionEvent
{
    private final Throwable exception;
    private final SubscriberExceptionContext context;
    private final Object extra;

    public ExceptionEvent(Throwable exception)
    {
        this(exception, null);
    }

    public ExceptionEvent(Throwable exception, Object extra)
    {
        this(exception,null,extra);
    }

    public ExceptionEvent(Throwable exception, SubscriberExceptionContext context)
    {
        this(exception,context,null);
    }

    public ExceptionEvent(Throwable exception, SubscriberExceptionContext context, Object extra)
    {
        this.exception = exception;
        this.context = context;
        this.extra = extra;
    }
}

只需继承guava EventBus,并编写自己的自定义EventBus。 提示:这个类应该写在com.google.common.eventbus包中,这样内部方法就可以被覆盖

package com.google.common.eventbus;

import com.google.common.util.concurrent.MoreExecutors;

public class CustomEventBus extends EventBus {

    /**
     * Creates a new EventBus with the given {@code identifier}.
     *
     * @param identifier a brief name for this bus, for logging purposes. Should be a valid Java
     *     identifier.
     */
    public CustomEventBus(String identifier) {
        super(
            identifier,
            MoreExecutors.directExecutor(),
            Dispatcher.perThreadDispatchQueue(),
            LoggingHandler.INSTANCE);
    }

    /**
     * Creates a new EventBus with the given {@link SubscriberExceptionHandler}.
     *
     * @param exceptionHandler Handler for subscriber exceptions.
     * @since 16.0
     */
    public CustomEventBus(SubscriberExceptionHandler exceptionHandler) {
        super(
            "default",
            MoreExecutors.directExecutor(),
            Dispatcher.perThreadDispatchQueue(),
            exceptionHandler);
    }

    @Override
    void handleSubscriberException(Throwable e, SubscriberExceptionContext context) {
        throw new EventHandleException(e);
    }
}