Solved

Java Message handling in Service Layer

Posted on 2016-10-13
3
66 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
3 Comments
 
LVL 15

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 26

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
micro services spring boot application error 3 199
Configure a Bean in an XML file 4 33
mysql jsp example issue 32 38
Java Restore security prompts not working 10 15
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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 …

821 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