This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.