Alan Evangelista
Alan Evangelista

Reputation: 3163

Vaadin: centralizing custom exception handling

I use Vaadin framework to create my web application GUI.

I have a Vaadin button and its click listener code may throw an application custom exception. In other points of application code, this exception is thrown all the way back to my custom window class, where it's centrally handled. I'd like to do something similar here: throw this exception in the clickListener code, so I could catch it in my custom terminal error handler in the window class. As the click listener class does not throw any exception, I'm unable to throw the exception there and I think I'll be obliged to handle the exception locally. As I don't want to handle the exception at the button click listener level, I think I'll forward it to my custom window class. Something like this:

Button btnNew = new Button("New", new Button.ClickListener() {
  public void buttonClick(ClickEvent event) {
    try {
        doThingThatThrowsException();
    } catch (Exception exc) {
        window.handleCustomException()
    }
  }
});

Is this the usual way of centralizing handling of a custom exception using Vaadin?

Upvotes: 2

Views: 1926

Answers (1)

Charles Anthony
Charles Anthony

Reputation: 3155

I can't answer as to whether it's the usual way of handling the exception, but we do something very similar.

Depending on how many listeners or how often you have to do this, you could also create abstract listeners that do that delegation for you.

e.g.

abstract class BasicListener {
  protected void handleThrowable(Component.Event event, Throwable throwable) {
    Window window = event.getComponent().getWindow();
    if (window instanceof ExceptionHandlingWindow) {
      ((ExceptionHandlingWindow) window).handleException(throwable);
    } else {
      // Log it ? DO something, anyway.
    }
  }
}

abstract class ExceptionHandlingButtonClickHandler extends BasicListener implements Button.ClickListener {
  @Override
  public void buttonClick(Button.ClickEvent event) {
    try {
      handleClick(event);
    } catch (Throwable e) {
      handleThrowable(event, e);
    }
  }

  protected abstract void handleClick(Button.ClickEvent event);
}

Button btnNew = new Button("New", new ExceptionHandlingButtonClickHandler() {
  @Override
  protected void handleClick(Button.ClickEvent event) {
    doThingThatThrowsException();
  }
});

Upvotes: 1

Related Questions