Is there a way to catch all unhandled exceptions thrown by a given class?

后端 未结 6 2078
你的背包
你的背包 2020-12-18 13:21

I know how to catch all unhandled exceptions in a given thread, but wondering if there is a way to catch all unhandled exceptions thrown by a given class in

6条回答
  •  天命终不由人
    2020-12-18 14:23

    Based on your comment to @Brians answer:

    I need to catch fault exceptions in a (wcf) service proxy in order to harvest a meaningful description

    Don't do that. If you want a meaningful message, then throw your own custom exception (you could also use one of the framework's exceptions, but using your own is better). Catch the System exception at the point where it is thrown (i.e FileNotFoundException, SQL exceptions, etc), rethrow as your own custom exception.

    and re-throw for the upper tiers can handle it as they see fit

    At the service boundary you can catch your custom exceptions (because you know exactly what you are looking for, you can catch on a base exception to get all derivatives), then strip your message out and package it up in a suitable way and return it to the caller.

    Or better still you could just use the IErrorHandler interface (MSDN doco here).

提交回复
热议问题