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

后端 未结 6 2057
你的背包
你的背包 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:06

    I think you should look to the Enterprise Library or PostSharp tools. For example you can use Enterprise Library and write custom exception handler, that will handle all exceptions (Or only some of them) and log them for example or write user-friendly message. But I think this approach should be used only if you want implement logging, or some data fall back (revert). And you should always rethrow them to UI layer that should show user-friendly messages.

    Enterprise Library and similar tools makes wrappers, as EFrank suggested, but they are generating them automatically, and these wrappers are transparent so you just calling methods of your class, and you even could not know that you are working with proxy.

    And Enterprise Library has WCF support, so as I think, this is should be your choice

    0 讨论(0)
  • 2020-12-18 14:08

    Try ELMAH it will handle any unhandled exception https://code.google.com/p/elmah/

    0 讨论(0)
  • 2020-12-18 14:16

    If it is about WCF exceptions, I would recommend plugging a dedicated behavior into the WCF pipeline. I have written a detailed example here

    It is based on two interfaces IErrorHandler and IServiceBehavior, it is also usable as an attribute and in file-based configurations.

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2020-12-18 14:25

    No, exception handling is closely tied to threads as threads execute code - classes do not.

    Also, there's no reason to wrap all calls in try/catch. Without knowing your code, that is most likely not the right thing to do. Exception handling frees you from handling each and every error locally. Embrace that and your code will be a lot simpler.

    0 讨论(0)
  • 2020-12-18 14:25

    I don't know of any way to catch all unhandled exceptions thrown by a given class.

    In order to achieve what you want to do, one thing is to create a wrapper class that calls into the original class and catches all exceptions. Then in the original class, you can use the wrapper class, without having to write the try catch blocks every time.

    0 讨论(0)
提交回复
热议问题