I’m working on EJB client + server, and I wonder, how to deal with server data errors on client. Should I check return value on client or catch an Exception? Example, that use return value logic:
//server bean method
public int create(MyObj obj) {
int PKID = someDataService.create(obj);
return PKID;
}
//client
if(!(MyBean.create(obj) > 0)) {
showMessage("Can't create MyObj");
}
Example with exceptions:
//server bean method
public void create(MyObj obj) {
int PKID = someDataService.create(obj);
if(!(id > 0)) {
//only EJBExceptions will be delivered to client
throw new EJBException(new MyDataException());
}
}
//client
try {
MyBean.create(obj);
}
catch(EJBException e) {
if(e.getCause().getClass.equals(MyDataException.class)) {
showMessage("Can't create MyObj");
}
else {
showMessage("Some boring error occurred");
}
}
I know, that return value checking instead of exceptions looks like coding in C, but all this EJB thing confuses me. Which is the better way?
Throw exception from your method
and annotate your custom exception with
ApplicationException