Is there a way to catch all unhandled exceptions t

2020-02-11 09:40发布

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 instead of wrapping each of the calls in a try catch block.

In case there's no way of doing this (likely to be the case) how would you achieve the same effect?

Just to give a bit of context, I am using a custom coded service proxy that decouples the rest of the app from the service data contract (WCF). I basically need to catch the faults so that I can extract specific fields (inner descriptions etc.), package them up into an custom exception and throw it again.

Any help appreciated.

6条回答
家丑人穷心不美
2楼-- · 2020-02-11 09:47

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.

查看更多
对你真心纯属浪费
3楼-- · 2020-02-11 09:52

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

查看更多
孤傲高冷的网名
4楼-- · 2020-02-11 09:55

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).

查看更多
Summer. ? 凉城
5楼-- · 2020-02-11 10:00

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.

查看更多
Juvenile、少年°
6楼-- · 2020-02-11 10:01

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

查看更多
Rolldiameter
7楼-- · 2020-02-11 10:09

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.

查看更多
登录 后发表回答