Hi there, quick question for the forum:
Say I have a bean class, MyBean, in which I have a biz-logic method, MyMethod. MyMethod throws a MyException class exception if something goes wrong.
The problem I'm wrestling with is that MyBean's remote interface only allows me to define MyMethod as throwing a RemoteException. As such, the client only sees RemoteException's and not MyException's. What piece of the puzzle am I missing to get the MyException class thrown to the client?
I would be most grateful for any advice.
William, there is no restriction on supplying business specific exceptions, infact they are very important to building a good system.
Your beans can throw any number of exceptions of any type, as long as they (and RemoteException) are declared in the remote interface, and your client has these exceptions in his classpath.
The practise I follow is to make my own business exceptions for my business needs, and throw EJBException for any system level failure (the app. server should wrap EJBException as a RemoteException for the client.
In your RemoteInterface do you throw each individual business exception or do you throw a common superclass of the business exception? For instance, do you use
public myBusinessMethod() throws RemoteException, BizException1, BizException2, BizException3 ....;
or, do you have a common superclass for all the BizExceptions like say CommonBizException & declare your remote interface as
public myBusinessMethod() throws RemoteException, CommonBizException;
Will this design work?
I throw each business exception separatly, however there is no reason why you shouldn't be able to throw a common super class of a business exception.
The only bad part about the superclass method is the extra code you will have to write on the client side to determine exactly which business exception the superclass represents.