Question

Can I use spring DataAccessException directly in service layer?. Is this a good practice/design to spread a frame work class in service layer?.

OR

should i catch DataAccessException in dao layer and rethrow it as some more generic Exception?

Was it helpful?

Solution

I think it's fine for the DAO layer to throw that exception. The service layer already knows about the persistence layer, so no additional dependencies are created.

OTHER TIPS

It is good to map/wrap the DataAccessException (thrown from DAO layer) into application specific exception hierarchy (in service layer) so that the dependent/calling layer has to just deal with your application specific exception classes.

Personally, I catch all checked exceptions in the service layer and throw my own ServiceExceptions initializing them with the catched exception. This way, the exception information is not lost and Controllers don't need to deal with low-level exceptions. But there's no need to do this in the DAO layer.

There's a pretty good chapter on exception handling in "Effective Java" (J. Bloch), it's a good read, as well as the rest of the book. Item 61 deals with this question.

http://books.google.de/books/about/Effective_Java.html?id=Ft8t0S4VjmwC

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top