Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJBs and Exceptions

 
JeanLouis Marechaux
Ranch Hand
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
blr sam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18304
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
JeanLouis Marechaux
Ranch Hand
Posts: 906
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic