Solved

Java Message handling in Service Layer

Posted on 2016-10-13
3
80 Views
Last Modified: 2016-10-18
I´m designing a service layer in a multilayerd architecture (Service, Business, Dao). Suposse I need a method like getUserAccounts

This method just return a list of Accounts, but what if my dao layer can´t connect to database, or if I have some error when reading the recorset, a parse problem for example. Or maybe another error in the business layer.

Do I have to catch the exception in the service layer, and map to a error code?
Do I have to catch the exception in the business layer, and the buisness layer return a bean with the error code? Avoiding to have any kind of logic in the service layer, more than exposing the Rest API
0
Comment
Question by:anonymous
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 16

Accepted Solution

by:
gurpsbassi earned 500 total points
ID: 41842543
Throw an exception and handle it in your rest layer with a exception handler.
You don't need to map all the error codes as you described. A generic error message is sufficient.
No user of your application would be interested to know the update failed because it violated referential integrity.
1
 

Author Comment

by:anonymous
ID: 41842563
Great! If the exception is in the DAO Layer should I catch it in the Business layer also? or only in the service layer?
0
 
LVL 27

Expert Comment

by:dpearson
ID: 41842935
You should always catch Exceptions at the level of the system that knows how to correctly handle that exception.

In other words, "it depends".  If you know what to do at the business layer to handle the exception (e.g. database offline, let's check the cache) then handle it there.

But if you need to return to the user "it didn't work" you should let it bubble up to the service layer and send the error out to the user.

Doug
1

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question