Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception Handling in a Layered Application

If I have a layered application and my Data Layer may encounter an OptimisticConcurrencyException how should the calling layer or indeed the lower layer deal with this??

The calling layer has no idea what an OptimisticConcurrencyException is, so should I be implementing a custom exception and catching the OptimisticConcurrencyException and throwing my Custom Exception?

Some guidance on this would be greatly appreciated.

like image 922
David Avatar asked Dec 17 '10 19:12

David


People also ask

Which layer is responsible for exception handling?

In the data access layer, the generic exception handling accounts for 93.50% (15,929) of the violations.

What are the 3 approaches to handling exceptions in a web application?

try catch finally 2. Use error events to deal with exceptions within the scope of an object. Page_Error Global_Error Application_Error 3. Use custom error pages to display informational messages for unhandled exceptions within the scope of a Web application.

What is application of exception handling?

Exception handling is the process of responding to unwanted or unexpected events when a computer program runs. Exception handling deals with these events to avoid the program or system crashing, and without this process, exceptions would disrupt the normal operation of a program.


2 Answers

OptimisticConcurrencyException is an exception that belongs to the data layer and should therefore be contained within it. It's unlikely that calling layer could handle it.

I would create a more generic exception such as DataSourceException which would contain some context such as which function failed (and with which parameters) and why. I would also include the original exception as inner exception.

Read my blog entry about catching exceptions: http://blog.gauffin.org/2010/11/do-not-catch-that-exception/

I've also written some other entries about exception handling, click on the Exception tag.

Update

I would create the following projects:

  • Project.Web <--- uses service through the specification interfaces.
  • Project.Service <-- implements everything in Specification
  • Project.Service.Specification <--- Place exceptions (classes) here and interfaces defining your services (or repositories if you do not use any services).
like image 181
jgauffin Avatar answered Oct 16 '22 18:10

jgauffin


You need to look at this from the perspective of the calling layer. The layer has asked the data layer to perform a job. If the OptimisticConcurrencyException can be handled by your data layer and the contract adhered to, then by all means catch it and then carry on and complete the job.

If, however, this is fatal to the job you're being asked to do, and if the caller is not expecting it, or doesn't know it, then it's fine for you to create your own exception class, catch the exception that is alien to the calling layer, and throw that instead. This is something that the calling layer can know about, and can be well-documented as a side-effect of using this function/API.

like image 4
Moo-Juice Avatar answered Oct 16 '22 17:10

Moo-Juice