I always wonder at what levels of my application whoudl I write try-catch
?
DAL? Cache? BL? UI-Logic?
If I write to log and re-throw it
should I use try-catch in every function?
aswuming any function can have an exception I didn't think of
I always wonder at what levels of my application whoudl I write try-catch
?
DAL? Cache? BL? UI-Logic?
If I write to log and re-throw it
should I use try-catch in every function?
aswuming any function can have an exception I didn't think of
Well, it depends. In the UI layer, I catch all errors globally in Application_Error, and handle these accordingly. I only then try-catch errors that I do not want to bubble up to the UI and cause a redirection to the generic error page. This has been effective for me in reporting most, if not all, errors.
Some people handle errors differently; they'll catch errors in the business layer, and either log and return them from the BLL, or log and rethrow a generic error. For instance, check out how the Enterprise Library Exception block approaches errors.
You can even use AOP library like PostSharp to attach to all the objects you want to handle errors for, or use MVC's exception filtering for handling errors too.
I personally tend to use way more try-finally than try-catch blocks (except for some external Data sources calls)
I keep try-catch for endpoint of my code, where I can log the error stack, and deal with error messages if necessary.
On a side note, be sure to just call throw;
so as not to swallow any exception.