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.
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.
Try ELMAH it will handle any unhandled exception https://code.google.com/p/elmah/
Based on your comment to @Brians answer:
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.
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).
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.
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
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.