• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • 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...
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • 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:
 
Aaron Robinson
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Shriram Shivakumar
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • 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).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!