aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Messages vs. Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Messages vs. Exceptions" Watch "Messages vs. Exceptions" New topic
Author

Messages vs. Exceptions

John Molitor
Ranch Hand

Joined: Nov 08, 2004
Posts: 39
Taking an SOA approach using EJB's. Client code calls a method to retrieve a list of objects. If the client passes in invalid criteria, we would like to return messages regarding what is invalid. However, if the call was succesful and has objects in the collection we would just like to return the collection of objects.
We could:
1. Return a complex object that contains a collection of objects and a collection of messages. The client would then need to try to retrieve the collection from the complex object (extra step). If the collection is empty, the client could retrieve the collection of messages to find out why the collection is empty.
2. If the collection of objects is empty, throw an ObjectNotFoundException, even though having an empty collection may be a valid scenario. Our "custom" exception could contain a list of messages.

3. Just return the empty collection, and let the client figure out what went wrong.

Thoughts or suggestions.
Thanks,
Chris
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
How about:

On success, just return the collection (populated or empty).

On failure, throw an exception that contains the collection of messages.

This sounds too obvious an answer. Am I missing something ?


kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
John Molitor
Ranch Hand

Joined: Nov 08, 2004
Posts: 39
Thanks for the Reply Ken.
Are you suggesting that "success" is a succesful call to the EJB, and "failure" is when an error occurred calling the service?
I guess part of my question is when is an exception an exception?
Here's a more detailed example (may or may not help) .
A client requests a list of dishwasher manufacturer's for the country of Romania. In our application, we have no dishwasher manufacturer's for the country of Romania. We would also like to send a mesage back to the client saying "No dishwasher manufacturers for the country of Romania."
Our application, could:
1. Return a empty Collection, since the call to the service was really successful, and let the client examine the collection, see it is empty and then deal with it.
2. Throw a checked exception that contains that contains the message. This would force the client to "deal" with the fact that the collection is empty, even though the call to service was really successful and no error occurred in the service execution.
3. Throw no exception and just return a DTO object that contains an empty list of dishwasher manufacturer's and a message string that contains "No dishwasher manufacturers for the country of Romania."
Thanks,
Fran
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Fran Hesser:
1. Return a empty Collection, since the call to the service was really successful, and let the client examine the collection, see it is empty and then deal with it.


I would do exactly that. After all, finding none isn't exceptional - it is a totally valid answer, and it's easy for the client to handle specifically if he wants to.

Also, messages shouldn't be the responsibility of the server - different clients could want to present different messages to their users (or react by playing a special sound, using a different color etc. pp.) - and you neither want your server to know about that, nor your client to need to parse a string to find out.

So just returning an empty collection sounds like the best solution in this case.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
John Molitor
Ranch Hand

Joined: Nov 08, 2004
Posts: 39
Thanks for the response Ilja. That's the direction we were leaning.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think I'm in the same place as Ilja here ...

We have many interfaces to partner systems over different protocols. At that layer an exception means a communication failure ... MQ-Series, SQL, HTTP or whatever. Anything that gets to the partner system and back is good. A result like "contract not found" might be a business disaster or it might not so we can't decide in this layer to throw an exception or even call it an error.


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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Messages vs. Exceptions