Exception Handling in Controller

We are following MVC pattern - model view controller and flow is

Controller -> Service -> Dao -> JdbcTemplate -> MySQL

1. All the errors should be cached and thrown in controller layer ? (OR) Error should be thrown from the controller method and it should be handled in the front end - js or jsp files ?

2. Do we need to catch DAO exception in the service layer or just pass the thrown exception to the controller layer and let the controller handle the exception?

3. Customized exception can be thrown on DAO and in service layer. However does the final exception handling should be done in controller side or in service layer.

4. Does the controller should catch any exception and create the localized error message and just pass the error message as string and response type as error to the front end to handle the error in the front end ?

Software ProgrammerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

There is no "should" or "must". There are several paths to a destination.

1) Why do errors need to be cached? Errors can be thrown from the controller or the controller can simply return an error object (not throw an exception).

2) This is up to you. I usually catch DAO exceptions in the DAO layer and return a status code to the controller.

3) Again, this is up to you. Handle exceptions on DAO layer and return it to the controller or throw them in DAO and let the controller catch them. It is entirely up to you.

create the localized error message and just pass the error message as string and response type as error to the front end to handle the error in the front end ?

This is what I usually do.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Software ProgrammerAuthor Commented:
Do you say different Object on success and different object on Error ???
No, one object that is also composed of an error object. If there are errors then the error object is filled, otherwise it is empty/null.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Software ProgrammerAuthor Commented:
Can you help me with a code snippet ?
Something like this

public class CustomerResponseWrapper {
    Customer customer;
    Error error;

Open in new window

Software ProgrammerAuthor Commented:
Still not clear. May be a template of codes in all layers will be helpful
Sorry I cannot post production code here. The thought is that you have a Java object (a wrapper) which contains both the actual response (lets say a Customer in my example) and an error response. Then you return this object to the client every time. If the error of the wrapper has value/is not null then it means that there was an error. By doing this you always return a 200 status code and the only thing the client has to do is to check if the error is empty/null.
Software ProgrammerAuthor Commented:
In general, couple of code snippet would be helpful to understand the solution and implement it. It would be hard to understand without sample code snippet or template.
I posted some sample code here.
Both are valid comments.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.