Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Throwing more than one application exception from a ejb interface

 
Jisha George
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there is a scenario where there are 4 application exceptions which will be thrown in a method. All the 4 exceptions have to be sent back to a client.

How can define the method signature in such a case.

Thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can declare a method to throw four types of exceptions and even have four statements that throw the four types. Is that what you were asking?

It sounded a bit like you wanted to throw all four in one call. Can't do that cause once you throw you're outta the method. You can chain exceptions if there were several things that went badly and you want to let the user know:

That's not quite right cause it might chain a null on a new exception, but I hope you see how it goes.
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
You can chain exceptions if there were several things that went badly and you want to let the user know:

That's not quite right cause it might chain a null on a new exception, but I hope you see how it goes.


Also, if the exceptions are independent of each other, doing the above will not be keeping with the intent of chained exceptions.

Could this be a code smell? Could the method that you want to throw four different exceptions be broken out into four different methods that each throw one exception? Maybe that will make your code more focused and show its intent more clearly.
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, "Jish",

Welcome to JavaRanch! We have very few rules around here and one that we're kind of particular about is that all visitors need to comply with the JavaRanch Naming Policy. Please edit your profile so that your publicly displayed name has at least two parts.

Much obliged pardner
 
Jisha George
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

Thanks for the replies. Let me elaborate my case.

Say I have an interface (EJB) method which does the following
1. Update information in DS1
2. Update information in DS2
3. Update information in DS3

Now, if the updation in DS1 fails, I still go ahead to update data in the next DS.

But finally I need to let the client know if any or all the updations have failed. So if its updation in DS1 & DS2 fails, I have to let the client know that there were exceptions in the method.

Note: - DS is a data source. All the data sources are of different types.

Thanks,
Jisha

P.S: By the way, I have my full name in there
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, my example was very bad for chained exceptions. This might be more reasonable:

That gives the user zero or many messages but no stack traces. Probably what they really want.
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had kind of the same idea as Stan although I would prefer to log each exception as it is caught, then add the exception message to a buffer. In the end check if the buffer is not empty and throw a new exception with that message.

 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing you might try is creating an AppException that can store all these other Exceptions for you.



Then in the code that catches the AppException, you'd iterate through the list returned by AppException.getCauses() and translate those exceptions into more user-friendly messages. Not sure what kind of, if any, issues this approach would have in a distributed environment though.
 
Jisha George
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your replies!!

I have got the solution !!

BTW, This was my first question posted on javaranch & I am pleased with my experience!!

Thanks Buddies !!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic