commit() throws a CommitException, which is not a runtime exception. Replace “RuntimeException” in your catch block with “CommitException” and you should be all good to go.
Hard to say what you are doing, because your code example is incomplete. I took your code and made it compile (note that I’m using Java 7) and it worked just as expected. There’s some piece that you are now leaving out.
public class PlaygroundUI extends UI {
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = PlaygroundUI.class)
public static class Servlet extends VaadinServlet {
}
public static class Bean {
String name;
public void setName(String name) {
if (name.equals("final")) {
throw new RuntimeException("Human readable message.");
}
}
public String getName() {
return name;
}
}
class Form extends FormLayout {
TextField name;
Button save;
BeanFieldGroup<Bean> binder;
public Form() {
name = new TextField();
save = new Button("Save");
binder = new BeanFieldGroup<PlaygroundUI.Bean>(Bean.class);
addComponent(name);
addComponent(save);
binder.setItemDataSource(new Bean());
binder.bindMemberFields(this);
save.addClickListener(new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
try {
binder.commit();
} catch (CommitException e) {
Notification.show(e.getMessage());
}
}
});
}
}
@Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
layout.addComponent(new Form());
}
}