Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guava EventBus: don't catch RuntimeException

I use guava's EventBus, unfortunately it catches and logs the InvocationTargetException that occurs when an event-handler throws a RuntimeException. Can i disable this behaviour?

like image 455
Fabian Zeindl Avatar asked Feb 10 '12 19:02

Fabian Zeindl


People also ask

How to use guava eventbus?

Let's start by using a simple example. 3.1. Setup We start by looking at the EventBus object. It can register listeners and post events. Using it is as simple as instantiating the class: Guava library gives you the freedom of using the EventBus in any way that best suits your development needs.

How to handle runtime exceptions in Java?

How to handle the Runtime Exception in Java? The Runtime Exception is the parent class in all exceptions of the Java programming language that are expected to crash or break down the program or application when they occur. Unlike exceptions that are not considered as Runtime Exceptions, Runtime Exceptions are never checked.

How to subscribe to an event using eventbus?

We can subscribe to an event by registering our EventListener class on the EventBus: 3.4. Unregistering Listeners If for any reason we want to unregister a class from the EventBus, that can also be easily done:

What is the difference between runtime exceptions and runtime errors?

Unlike exceptions that are not considered as Runtime Exceptions, Runtime Exceptions are never checked. The Runtime Exception usually shows the programmer's error, rather than the condition a program is expected to deal with.


2 Answers

As it stands, this is a deliberate decision, and discussed in the EventBus docs:

Handlers should not, in general, throw. If they do, the EventBus will catch and log the exception. This is rarely the right solution for error handling and should not be relied upon; it is intended solely to help find problems during development.

Alternative solutions are being considered, though I seriously doubt they'll make it into release 12.

like image 77
Louis Wasserman Avatar answered Oct 31 '22 02:10

Louis Wasserman


Here is code for lazy

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);
        }
    }
}

Now, you can subscribe ExceptionEvent.

Here is my ExceptionEvent for just copy&paste

@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;
    }
}
like image 30
wener Avatar answered Oct 31 '22 02:10

wener