• Post Reply Bookmark Topic Watch Topic
  • New Topic

Throwing system exceptions from the bean method

 
Alex Sharkoff
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'd greatly appreciate if you could share your thoughts on the following topic.

The EJB 2.0 spec (section 18.2.2) says that bean methods should always throw javax.ejb.EJBException when something unexpected occurs:

Any other unexpected error conditions should be reported using the javax.ejb.EJBException.


I have got a plain java class that has got all business logic and my session bean just simply delegates method calls to it (it allows to plug a biz logic into a different architecture; bean and plain java class both implement the same interface that declares business methods).
In some cases plain java class throws runtime exception java.lang.IllegalStateException and a bean lets it propogate to the Container (as defined in the section 18.2.2 of the spec)

My question is: if I was to copy all biz logic from plain java class into the session bean would I also have to replace all instances of java.lang.IllegalStateException with the javax.ejb.EJBException so that the bean code conforms with the spec requirements?

Thanks to all in advance.
[ June 23, 2004: Message edited by: Alex Sharkoff ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17290
9
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actaully what happens it that any Exception thrown that gets to the container automatically will be wraped by the container with an EJBException/RemoteException.

So whether the code that causes it is in a POJO (Plain Old Java Class) or directly in the SessionBean, it will still be wrapped by the Container, if it reaches the Container. In most cases I think you want to catch all the Exceptions and handle them before it reaches the container, this wy you cna see the stack trace better, and if the client needs to no something bad happens, you can send a more readible message back to the client.

Good Luck

Mark
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If java.lang.IllegalStateException is thrown, you need not do anything. Here is what the spec says in 18.2.2:
If the bean method encounters a RuntimeException or error, it should simply propagate the error from the bean method to the container (i.e., the bean method does not have to catch the exception).
 
Alex Sharkoff
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark and Roger for your replies.

We all agree that the Container is always going to wrap system exceptions into EJBException/RemoteException .

I am just wondering why does the spec explicitly say (section 18.2.2) that the bean code must report any unexpected errors as javax.ejb.EJBException and not as any runtime exception (eg, IllegalStateException)? Or am I just misunderstanding what the spec says?
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IllegalStateException is-a RuntimeException, so you can just let it propagate up to the container.

What the spec says is that anything other than RuntimeException, error or checked exception should be reported using javax.ejb.EJBException, although quite what sort of exception this could be is unclear to me.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!