1

I have an n-tier web application, and I want to catch a specific type of exception in every method coming from the DAL (Data access layer) and rethrow it as a new exception of a specific type.

There are many methods in my DAL, so I don't want to start wrapping each one with try/catch.

I think this is possible using the Exception Handling Application Blocks, but i couldn't find any good documentation of how to do this...
I'm not familiar with the previous versions of the application blocks neither.

gillyb
  • 8,760
  • 8
  • 53
  • 80
  • 1
    You don't want to catch *every* possible exception. You don't want to wrap exceptions that should be uncatchable (and in 4.0 actually are uncatchable, yupee!) like those for out-of-memory, A/Vs and stuff; and you also don't want to wrap exceptions that signal bugs and should never be thrown in production like those for de-referencing null, dividing by zero and bad arguments. – R. Martinho Fernandes Mar 24 '11 at 11:07
  • 1
    just don't do this, it creates more problems than it solves – jeroenh Mar 24 '11 at 11:15
  • Ok, i edited my question to be catching a specific type of exception. This could be good for catching a range of different exceptions, and wrapping them as a DalException persay... – gillyb Mar 24 '11 at 12:00

1 Answers1

1

Has your DAL repositories got an interface? I would implement the interface using a decorator pattern. All the decorator does catches the exception and then builds a new exception and throws that out to the upper tier

As a point of note, in our n-tiered applications we always let exception just get thrown naturally and catch them in once single place and log them. We only create specific exceptions if we absolutely have to and that would be rare enough.

The reason for this is maintainability of code. Code can easily become unreadable when try/catches exist everywhere.

bstack
  • 2,466
  • 3
  • 25
  • 38