Multiple error handlers


The project I’m working on already has a “custom” error handler but I have a task to make another one which handles only one exception. My question is can the two handlers co-exist and if yes - where do I set mine to be used by the project?


You can set only one instance of
per UI. But you can implement a handler that
to other handlers according to the exception type. For example:

public class ErrorHandlerDispatcher implements ErrorHandler {

public interface CustomErrorHandler extends ErrorHandler {
    boolean canHandle(Class<? extends Throwable> throwableClass);

private static List<CustomErrorHandler> handlers = new ArrayList<>();

static { // add all the implementations
    handlers.add(new ErrorHandler1());
    handlers.add(new ErrorHandler2());
    handlers.add(new ErrorHandler3());
    handlers.add(new DefaultErrorHandler());

public void error(ErrorEvent event) {
            .filter(handler -> handler.canHandle(event.getThrowable().getClass()))
            .ifPresent(h -> h.error(event));

[/code]You can create multiple implementations and add them to the
list. For example, if you want
to handle only
s, you can implement it as follows:

public class ErrorHandler1 implements ErrorHandlerDispatcher.CustomErrorHandler {

    public boolean canHandle(Class<? extends Throwable> throwableClass) {
        return throwableClass.equals(NumberFormatException.class);

    public void error(ErrorEvent event) {"Number format exception!", Notification.Type.ERROR_MESSAGE);