kevin
kevin

Reputation: 14065

Best Way to handle Exception in multi-tier

I got a question about exception handling. I guess it may be quite easy for you but for me I just don't know how to handle exception in multi-tier projects.

Let's say in my solution, I have several project.

I have (lower) DataAccess, BizComponent, WCF, Proxy and Presentation (upper).

I try to "try catch" in DataAccess and throw the exception to BizComponent and in BizComponent I try again "try catch" and log the error and throw the exception again to WCF.

In WCF and Proxy layers, I do the same thing. In presentation layer, I show come custom message to end user.

My senior told me that I only need to start doing "try catch" in WCF and upper layers. And I don't need to do in DataAccess and BizComponent because it will be caught in WCF.

Should I try to catch the exception in DataAccess,BizComponent and throw exception or should I just try to catch only in WCF and starts throwing to upper layers?

Which one is better practice ?

If you are not clear about my question, please let me know. This is my first multi-tier projects so it makes me confused. Thanks in advance.

Upvotes: 2

Views: 483

Answers (1)

detaylor
detaylor

Reputation: 7280

As a rule of thumb only handle (catch) exceptions if you are going to do something with them.

For example, if you could catch a database exception in the data layer if you wanted to provide additional information in the "upper" layers.

An approach I use personally is to catch and log exceptions in the business layer and then rethrow the same exception or a wrapping exception that provides more friendly information for layers higher in your stack. This provides a consistent logging process and does not require boiler plate logging code in the application.

If you reuse the code in multiple applications and need different log stores this can be handled using dependancy injection.

Upvotes: 2

Related Questions