I've a WCF wit开发者_运维问答h following 3 tiers:
- Service
- BLL
- DLL.
Could anyone please help me out on:
Which one of the above tier is the best place to catch and log the exception on following scenario:
Scenario 1:
Exception happens on DLL.
Scenario: 2:
Exception happens on BLL.
Also, should I always always wrap the call to BLL from Service on Try and Catch?
it depends on your system here, but I would log the exceptions probably inside the BLL. And mind you "log" not catch! That is - at the end of the catch will stand a rethrow!
I don't catch anything in the service at all - this is just to communicate with the client - no need to put any logic inside it IMHO.
And of course: catch your expected exceptions where they are thrown and just let the unexcepted pass.
For example: Scenario 1 - maybe catch SqlExceptions, try handle them and if not wrap them in some DAL-Exception and throw these. Scenario 2 - maybe catch the DAL-Exceptions from Layer 3 and see if you can handle those or wrap them yet again, ...
IMHO it's almost never a good idea to handle, log and mute all kinds of unexcpected exceptions (catch (Exception)
) - the app state will just be very undefined after this.
Have a read through some of the articles on MSDN about the Microsoft Exception Handling Block
It goes through a lot of scenarios and give you some good ideas for exception handling strategy best practices regardless of whether you use the exception handling block or not, as shown in the diagram below.
精彩评论