Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Messages vs. Exceptions

 
John Molitor
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
John Molitor
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Molitor
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response Ilja. That's the direction we were leaning.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic