Exception handling using an HttpModule

前端 未结 6 1447
渐次进展
渐次进展 2021-02-10 04:24

We\'re reviewing one of the company\'s system\'s exception handling and found a couple of interesting things.

Most of the code blocks (if not all of them) are inside a t

6条回答
  •  名媛妹妹
    2021-02-10 04:53

    Check out ELMAH. It does what you're talking about. Very well.

    When I create libraries I try to always provide a reduced number of exceptions for callers to handle. For example, think of a Repository component that connects to a sql database. There are TONS of exceptions, from sql client exceptions to invalid cast exceptions, that can theoretically be thrown. Many of these are clearly documented and can be accounted for at compile time. So, I catch as many of them as I can, place them in a single exception type, say a RepositoryException, and let that exception roll up the call stack.

    The original exception is retained, so the original exception can be diagnosed. But my callers only need to worry about handling a single exception type rather than litter their code with tons of different catch blocks.

    There are, of course, some issues with this. Most notably, if the caller can handle some of these exceptions, they have to root around in the RepositoryException and then switch on the type of the inner exception to handle it. Its less clean than having a single catch block for a single exception type. I don't think thats much of an issue, however.

提交回复
热议问题