wood burning stoves 2.0*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Throwing more than one application exception from a ejb interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Throwing more than one application exception from a ejb interface " Watch "Throwing more than one application exception from a ejb interface " New topic
Author

Throwing more than one application exception from a ejb interface

Jisha George
Greenhorn

Joined: Apr 30, 2004
Posts: 4
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

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 - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

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

Joined: Apr 30, 2004
Posts: 4
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

Joined: Jan 29, 2003
Posts: 8791
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

Joined: Feb 26, 2001
Posts: 4442
    
    5

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

Joined: Feb 26, 2001
Posts: 4442
    
    5

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

Joined: Apr 30, 2004
Posts: 4
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 !!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Throwing more than one application exception from a ejb interface
 
Similar Threads
ejbcertificate mock example
system exceptions
JVM, programmatic, runtime exceptions
question on assertion
What is the best way to handle Exception in Java?