Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

EJBs and Exceptions

 
Ranch Hand
Posts: 906
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 27926
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic