• 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

Is there a way in Java to Throw an exception and return an object too

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Stateless Session EJB which process creditcard authorization.
My requirement is to throw Exception ( for example SQLException if there is a database error) from my EJB public methods so that java clients can catch exception and see the error messages and also I have to return an object which contains all other information about CreditCard Information.
I have to return this object even if there is a database error and I am throwing an exception for it...
so my question is, is there a way in Java to both throw an exception and return an object simultaneously ??? so that clients can catch exception and can recieve a return object...
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The usual way to do this sort of thing is to return a customized exception which contains your data so far:
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must take care when throwing exceptions from ejbs as the transaction management will be affected by certain transacytions.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will "finally clause" in "try catch finally" help. Because finally clause code will always run.
[Please note that code in finally clause runs even if catch clause has a return !]
So, if you build the object to be returned in "finally" clause, and return that object, then client will have exception as well as a returned object.
Please let me know if I am wrong.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right. The 'finally' part will be executed even if you have a return in 'catch' and it will be executed even if the 'catch' block is not executed i.e., even if there is no exception, in this case, it will create an exception object and return it. Thats not a nice way of doing things

[This message has been edited by Shriram Shivakumar (edited August 21, 2000).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic