If a business nethod in the component interface declares an application exception, is the corresponding bean class method allowed to declare a subclass of that exception? I read that the bean class method should not declare any application exceptions not declared by the interface method. But I have a small doubt about the subclasses of the declared exceptions. Are they allowed? I expect your answer to be NO. If that's the case, this error will be detected at deployment, right? Would your answer change if the bean class were to actually implement the component interface? Will it still be an error detected at deployment? Hope you remember that if you implement an interface, you can declare sub-classes of that exception in the implementing method in the implementation class, but I guess that should not be allowed with EJB's
I think the bean class methods can declare exception of subclass-type of the application exception declared in component interface without causing a deployment-time or compile time error (regardless of whether the bean class is actually implementing the component interface - which ofcourse is not recommended). This should work as per the plain old java method overriding rules.
One example of this is with the Finder methods in entity bean's home interface. We declare the FinderException in entity beans home interface's finder methods but the actual finders implemented by the container will throw ObjectNotFoundException (subclass of FinderException - checked, application exception). Same way it should work for any business method of component interface/bean class.
SCJP, SCWCD, SCBCD
posted 14 years ago
Thanks Pranav, but still I am not convinced. I understand what you say. We write the home interface with a method that throws FinderException. The container implements the home class and the finder method. Of course, if a method is declared to throw an exception, that method can also throw sub-classes of that exception. We are talking about the home class here. But still the bean class methods corresponding to what ever methods you declare in the home / component interface cannot throw any checked exceptions than what is declared. I think that would include sub-classes of those checked exceptions.
If you have this in the component interface : public void doSomething() throws ABCException
and If you write a concrete class that implements this, then that implementing class doSomething() method can throw sub-classes of ABCException. This is plain java. But this is not what I am asking
Can you have public void doSomething() throws XYZException in the BEAN CLASS
where XYZException is a sub-class of ABCException.
Although I could not locate a reference in the spec that tells you that the bean class method can throw an exception that subclasses the exception that is stated in the throws clause of its component interface, I think it might still work, albeit I am not sure if this is guaranteed to :roll:
This is just a finding -> The code which container generates that actually calls the methods implemented in the bean class would typically provide a catch clause (or clauses) for handling the exception(s) that you throw by reading the 'throws' section of the component interface method definition.
Component interface: public void doSomething() throws ABCException Bean class: public void doSomething() throws XYZException where XYZException is a subclass of ABCException
The container generated code for the class that implements the component interface would normally do this.