Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put try catch

Tags:

Consider this scenario: I have 3-layer app, when the user click on the button the button event handler calls a method in biz layer that do whatever with data my button event handler supply and then pass that data to the data Access layer which sends them to the backend database. The question is where to put the try catch? In the data layer, in biz layer, in presentation layer or maybe put it around all of them? What the best strategy to represent exception handling as in this scenario?

like image 326
Abdullah BaMusa Avatar asked Feb 07 '09 14:02

Abdullah BaMusa


2 Answers

We follow

Do not catch exceptions if you do not know what to do with it.

We never handle any exceptions which we cannot handle or default. It gets bubbled up and we handle it at the application level.

Say if you can default a value, for a businesss object, then you can handle it in the business layer.

like image 72
Ramesh Avatar answered Sep 30 '22 05:09

Ramesh


definitely put a try catch closest to the user -- because in that location you can translate it into something meaningful for your user.

Deeper try catches could only be needed if you intend to do something with them -- for example, handle the exception, or perhaps log and rethrow the exception.

like image 25
Leon Bambrick Avatar answered Sep 30 '22 04:09

Leon Bambrick