aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes EJBs and Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJBs and Exceptions" Watch "EJBs and Exceptions" New topic
Author

EJBs and Exceptions

JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
Hi.
In an EJB, I can catch an XXXException and throw an EJBException.

What is the difference (and the better approach) to rethrow the EJB Exception ?
option 1 )
or
poption 2 )

The caller will receive a RemoteException(re)
Does re.getMessage(); give the same result whether I've used option 1 or option 2 to construct the error ?


/ JeanLouis<br /><i>"software development has been, is, and will remain fundamentally hard" (Grady Booch)</i><br /> <br />Take a look at <a href="http://www.epfwiki.net/wikis/openup/" target="_blank" rel="nofollow">Agile OpenUP</a> in the Eclipse community
blr sam
Greenhorn

Joined: Jan 15, 2002
Posts: 7
Hi Bill,
With respect to Exceptions in EJB i found a very good article @ http://www-106.ibm.com/developerworks/java/library/j-ejbexcept.html
I am sure you will find the answer your looking for here...
Cheers
Sham
JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
Thanks Sam.
I've already read this article a few weeks ago. It's very good indeed, but it does not answer my question.
Actually, it does not show how the EJBException is handled and used by the caller.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16065
    
  21

I think you're making this too hard. Unless I'm mistaken, in EJB1.1, if a non-EJB-type exception is thrown in an EJB, the container transports it intact to the client.
Consider a simple example. Let's force a Divide By Zero exception (A NullPointerException's easier, but too likely to obscure other problems).
First, define the method in the EJB to include java.lang.ArithmeticException as one of the exceptions it may emit.
Secondly, make sure that the exported definition for that method ALSO indicated that a java.lang.ArithmeticException can be thrown.
Now in the client code:
try {
myEJB.divide( 1234, 0 );
} catch ( java.lang.ArithmeticException ex ) {
System.out.println("GOTCHA!");
System.out.println(ex.getMessage() );
ex.printStackTrace();
}
Hopefully you won't do THIS in real life, but it's often useful to be able to define user exception classes and throw instances of them. Just remember that the class definition has to be defined in the CLIENT's classpath and well as the EJB's classpath or the user exception will trigger a system exception!


Customer surveys are for companies who didn't pay proper attention to begin with.
JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
Tim,
I think each Application exception is rethrown by the container to the client, so you're right, there's no need to worry about it.
But System Exceptions are a bit different.
My understanding is that according to the spec, a bean method should throw an EJBException when it catches a system exception.
Then the container "transform" it to a RemoteException, which is what the client receives.
So my question is actually about "System exceptions". What is the better way to handle them in order to be able to use them from the client ?

Am I wrong somewhere ?
[ June 10, 2002: Message edited by: Bill Bailey ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJBs and Exceptions