my dog learned polymorphism*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes SystemExceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "SystemExceptions" Watch "SystemExceptions" New topic
Author

SystemExceptions

Peter Warde
Ranch Hand

Joined: Aug 18, 2004
Posts: 71
Can anyone help

Is it the case that a system exception such as IllegalArgumentException thrown by say getUserTransaction() in an entity bean will be thrown by the container to the client as RemoteException (or EJBException in the case of a local interfaces) or as an IllegalArgumentException?
Paul Maessen
Greenhorn

Joined: Sep 27, 2004
Posts: 12
As RemoteException (or EJBException in the case of a local interfaces), with one exception:

If the bean method runs in a transaction that the client started the client will receive a TransactionRolledbackException or ransactionRolledbackLocalException
Nileesha Bojjawar
Ranch Hand

Joined: Oct 10, 2003
Posts: 85
Peter,
I have a doubt, Did u mean IllegalArgumentException or IllegalStateException because i read that getUserTransaction() if called in a CMT Entity bean will throw IllegalStateException.

Regards,


Nileesha<br />SCJP 1.4, SCBCD, SCWCD.
Peter Warde
Ranch Hand

Joined: Aug 18, 2004
Posts: 71
Sorry - I meant an IllegalStateException.
Nileesha Bojjawar
Ranch Hand

Joined: Oct 10, 2003
Posts: 85
According to the Spec it says that IllegalStateException must be thrown but the container implementation might throw and more general Exception than the specific one, so it might throw a RemoteException/EJBException.I am confused for the same reason, if the exam question gives both the generic and the specific Exception as optinos,which one must be chosen?
Paul Maessen
Greenhorn

Joined: Sep 27, 2004
Posts: 12
Hi Nileesha,

Maybe this helps:

IllegalStateException is thrown by the container to the *bean* (eg if the bean code makes a call to getUserTransaction() that is not allowed).

If the bean code doesn't handle the exception the container will throw a RemoteException/EJBException to the client.

see tables in spec 18.3
Nileesha Bojjawar
Ranch Hand

Joined: Oct 10, 2003
Posts: 85
Thanks Paul,
Makes it more clear now!!
Peter Warde
Ranch Hand

Joined: Aug 18, 2004
Posts: 71
Just be clear - is this true of all Runtime and System exceptions. That the runtime exceptions such as NullPointerException or one of the subclasses of EJBException NoSuchObjectLocalException, or a subclass of RemoteException eg NoSuchObjectException etc are thrown to the bean as is but thrown by the Container to the Client as a more generalized EJBException or RemoteException.

I think the above is true, though someone in this forum somewhere recently said the exception to this was TransactionRolledBackException and it's local version. Can anybody tidy all this up to give an overall set of rules?
Paulus Maessen
Greenhorn

Joined: Sep 29, 2004
Posts: 12
Hi Peter,

Below as I understand it (it’s a bit lengthy, but you asked for it ):

If you are calling bean methods directly, or methods on other objects like e.g. SessionContext, exceptions are thrown as-is since the container is not involved. Just plain old Java.

If you are calling bean methods through the home/component interface however, the container cicks in and calls the bean method on behalf of the client. Now the following rules apply:

- if the bean method throws an application exception the container re-throws it as-is to the client
– if the bean method throws any other type of exception the container throws a RemoteException or EJBException or subclass thereof UNLESS the beans client started the transaction, in which case the container throws TransactionRolledbackException or TransactionRolledbackLocalException

An application exception is an exception that is declared to be thrown by the home/component method that is a (indirect) subclass of Exception but not of RuntimeException (+ some exceptions to this rule, see spec).

So:
- runtime exceptions like IllegalStateException and NullpointerException can never be application exceptions and the container will always throw Remote/EJBException when the bean throws such an exception.
- system exceptions may be re-thrown to the client, but only if they are explicitly included in the throws clause of the home/component interface
- If a bean method makes a call to a remote interface, any RemoteExceptions or subclasses thereof thrown from the remote method must be handled by the bean code because bean methods are not allowed to throw RemoteExceptions
- If a bean method makes a call to a local interface and the bean code doesn’t handle EJBExceptions or subclasses thereof : the container is required to throw a Remote/EJBException. I suppose that in the case of a local interface the container could just re-throw the exception, but I don’t think it is required to do so.

Hope this tidies it up,

Paul

- SCBCD
Sandesh Tathare
Ranch Hand

Joined: Jun 22, 2003
Posts: 82
Gr8 job Paul!

I would like to comment on what you have written:


- system exceptions may be re-thrown to the client, but only if they are explicitly included in the throws clause of the home/component interface


I tried calling a business method of a Stateless Session Bean from remote client. The throws clause of business method had RemoteException and NullPointerException. Client did not receive NullPointerException but got RemoteException with NullPointerException as nested exception.

I remember some one saying Container may throw Generic Exceptions instead of Specific Exception like CreateException instead of DuplicateKeyException. But that is certainly not applicable in the above mentioned case.

So to summarize it let me say:

System exceptions are wrapped in RemoteException and client receives RemoteException even if system exception is mentioned in throws clause of a method of a bean.




Regards,<br />Sandesh<br />(SCJCP, SCWCD, SCBCD - 99%, OCP-1)<br /> <br />Either find a way or create one.
Paulus Maessen
Greenhorn

Joined: Sep 29, 2004
Posts: 12
Sandesh,

Thank you for correcting me: I got the terminolgy mixed up.


- system exceptions may be re-thrown to the client, but only if they are explicitly included in the throws clause of the home/component interface


should read

- checked exceptions are re-thrown to the client, but only if they are explicitly included in the throws clause of the home/component interface

btw, this means that if an exception like java.sql.SQLException is declared in a throws clause it wil not be wrapped in a RemoteException

regards,

Paul

- SCBCD
Rahul Devgan
Greenhorn

Joined: Aug 04, 2004
Posts: 24
just wanted to add one line here that exceptions local to the bean like SQLExceptions and others of similar nature should be avoided in throws clause of Home/Compnt Iface as they hold no meaning to the client.

This approach should be avoided even for debugging purposes.

All such exception should lead to throwing a EJBException/Remoteexception only.


Rahul Devgan<br />SCJP<br />--- one must be willing to let go of small pleasures in life to achieve greater things ---
 
Consider Paul's rocket mass heater.
 
subject: SystemExceptions
 
Similar Threads
Question over NumberFormatException
2 quick questions...
Problem with date insertion in MySQL
SCBCD 5.0 question about merging a removed entity
Detached entity doubt