aspose file tools*
The moose likes Java in General and the fly likes Throwing exception vs returning message Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Throwing exception vs returning message" Watch "Throwing exception vs returning message" New topic
Author

Throwing exception vs returning message

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi all, we have a scenario where we query the database for some entity. If the entity is not existing JPA returns null. We have to let the client know that the requested entity is not available because some one has already deleted it from the database.

We have Facade layer, underneath that we have a service layer and underneath that we have the DAO layer. Facade is implemented using REST. Our code does this

1. When the entity is null we throw a custom exception with a message passing in the exception's constructor.
2. This custom exception has an exception mapper which converts the 500 Internal Error to 404 exception and the message is also written to the response.

I argue that why are we throwing an exception when there is need to throw an exception. I said we can simply return null from DAO layer to the service. The service will then build the message string and pass it to the Facade and Facade returns the message.

To do this actually lot of our code has to change. Besides I want to know whether throwing and exception is good or going with the approach I described is good. I am just curious regarding the best approach.


Love all, trust a few, do wrong to none.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

So A calls B to fetch entity X, and when X is not present, B can either throw an exception or return null, yes?

Which one is correct depends on the semantics of "fetch entity X".

If it's "I know that X exists, so fetch it for me, and if somehow it's not there, that's wrong, and I can't proceed with my task without it" then it should throw an exception.

If it's "Give me X if it's there, but if not, I dont' really need it, I'll just move on without it," then returning null (or a Null Object) is appropriate.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

The JPA API method does not throw any exception. If the returned entity is null then we are throwing a custom exception.

I would like to know whether throwing an exception is good or returning null to service layer and service layer building a message and sending it to controller layer is good?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

chaitanya karthikk wrote:I would like to know whether throwing an exception is good or returning null to service layer and service layer building a message and sending it to controller layer is good?


I thought I answered that. I don't know what you're still unsure about.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Jeff Verdegan wrote:
I thought I answered that. I don't know what you're still unsure about.
Thank you Jeff. I dint understand it at the first instance. Now I understood. So if something I want is not there and I don't want to proceed with anything I will throw an exception. Or if something I want is not there and I am not bothered about it, then I will return null and proceed with other things.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Throwing exception vs returning message