• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using RemoteException and EJBException

 
Himai Minh
Ranch Hand
Posts: 1360
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On p.58 of Frits's notes, there are two guidelines about handling system exceptions:
"1. If the EJB method performs an operation that results in a checked exception that the bean method cannot recover, the bean method should throw the EJBException that wraps the original exception."
My comment : can we throw RemoteException that wraps the original exception instead?

"2. Any other unexpected error conditions should be reported using the EJBException."
My comment : can we throw RemoteException instead ?

I am reading Head First EJB 2.0 for fun now. On p. 539 of the book, it says a rule "RemoteException goes to remote clients , EJBException goes to local client".
It says "RemoteException is for remote client only.... EJBException is for local clients only."

Is the rule applied to EJB 3.0? In EJB 3.0, is there any clear guideline when the bean should throw RemoteException and when the bean should throw EJBException.
For example, if the bean fail to obtain a database connection or fail to look up in JNDI, which exception the bean should throw ? RemoteException or EJBException ? Or it doesn't matter.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himai Minh wrote:On p.58 of Frits's notes, there are two guidelines about handling system exceptions:
"1. If the EJB method performs an operation that results in a checked exception that the bean method cannot recover, the bean method should throw the EJBException that wraps the original exception."
My comment : can we throw RemoteException that wraps the original exception instead?

"2. Any other unexpected error conditions should be reported using the EJBException."
My comment : can we throw RemoteException instead ?

I am reading Head First EJB 2.0 for fun now. On p. 539 of the book, it says a rule "RemoteException goes to remote clients , EJBException goes to local client".
It says "RemoteException is for remote client only.... EJBException is for local clients only."

Is the rule applied to EJB 3.0? In EJB 3.0, is there any clear guideline when the bean should throw RemoteException and when the bean should throw EJBException.
For example, if the bean fail to obtain a database connection or fail to look up in JNDI, which exception the bean should throw ? RemoteException or EJBException ? Or it doesn't matter.


Use of RemoteException for explicitly throwing it has been deprecated. Beans now neither declare this exception in their throws clause nor do they throw it explicitly in any case. As per Section 4.9.6:

EJB 1.0 allowed the business methods to throw the java.rmi.RemoteException to indicate a non-application exception. This practice was deprecated in EJB 1.1—an EJB 1.1 or EJB 2.0 or later compliant enterprise bean should throw the javax.ejb.EJBException or another RuntimeException to indicate non-application exceptions to the container (see Section 14.2.2). An EJB 2.0 or later compliant enterprise bean should not throw the java.rmi.RemoteException from a business method.


The clients on the other hand will automatically receive RemoteException, EJBException, or EJBException depending on how they are invoking the bean and what exception does the bean actually throw.

HTH,
Paul.
 
Himai Minh
Ranch Hand
Posts: 1360
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, thanks for your answer.
So, does it mean java.rmi.RemoteException is not used in any bean method any more ? Should we only limit the usage of RemoteException in RMI only?
In this EJB 3.1 exam, will we be asked under what circumstances we should use RemoteException ?

The specification says "As EJB 2.0 or late compliant EJB should not throw java.rmi.RemoteExceptio from a business method". Does it mean with EJB 3.1, a bean must only throw EJBException for a system exception or any other application exceptions ( eg FileNotFound, IOException and etc)?

 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himai Minh wrote:Paul, thanks for your answer.
So, does it mean java.rmi.RemoteException is not used in any bean method any more ? Should we only limit the usage of RemoteException in RMI only?
In this EJB 3.1 exam, will we be asked under what circumstances we should use RemoteException ?

When you say "use", I am not sure what you mean. As I explained above, you need to know that a bean never explicitly throws RemoteException. RMI is completely transparent to the beans. It is not required for this cert either. So not sure what you mean by limit the usage in RMI.
However, you need to know when may a client receive a RemoteException. You may consider it a "use". Depends on your perspective.
 
Himai Minh
Ranch Hand
Posts: 1360
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To clarify my post, in RMI, a remote method throws java.rmi.RemoteException. A client application catches that RemoteException.

According to the spec, in EJB 2.0 or later, a bean should not throw RemoteException from any business method.

So that means a bean method should only throw an application exception or EJBException as a system exception ? It seems to me that with EJB 3.1, a bean method should never throw RemoteException to the client. The client of the bean catch the application exception thrown by the bean or an EJBException thrown by it.

According to Frits's notes, p.60 , the container may throw a RemoteException to the client. Or, the communication subsystem may throw a RemoteException to the client.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic