Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SystemExceptions

 
Peter Warde
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Peter Warde
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry - I meant an IllegalStateException.
 
Nileesha Bojjawar
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul,
Makes it more clear now!!
 
Peter Warde
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Paulus Maessen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic