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?
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 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.
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:
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.
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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With