Question

I am writing an application where I am confused how to communicate between the business layer and service layer. Let me clear my point by giving example:

createStusdentRecord is method in service layer, I am calling this from business layer. now approach 1 : Create different custom exceptions and throw if some data is missing etc, and on success return studentid, let the business layer handle these exception.

Approach 2 : Create one class SMD ( status message and data ) and handle all exception in service layer. Return this SMD to business layer, with no exception handling in business layer.

Which approach is better and why?

What should be approach when we expose web-services?

Was it helpful?

Solution

I would go with Approach 1. Assuming these truly exceptional conditions that the caller can handle.

I like exceptions because then there is a way to find out why something failed. Hiding all errors or just returning a boolean for success/fail is a bad practice.

The point is to try to hide as much internal stuff as possible to hide implementation details as well as stuff the other layers don't know about (or want to know about) but balance that with enough information if there is a problem.

Student s = dao.createStudent(...)

If the given parameters are invalid do you throw an exception? Maybe depends why they are invalid. Perhaps it's a good idea to make custom Exceptions for concepts such as "A Student with these parameters already exists". However, something like "parameter is invalid" might be a better choice to use one of the JDK built in exceptions like "IllegalArgumentException".

Furthermore, I would make all your custom Exceptions subclass a parent "DaoException" so client code that only cares about success/fail catch catch the parent, but something that wants more fine grained control can always catch subclasses.

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