views:

116

answers:

5

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.

+1  A: 

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 me a lot simpler.

Brian Rasmussen
cheers - my (web) app is rather huge and I cannot have a global handler because it would catch everything and I do not want that. I need to catch fault exceptions in a (wcf) service proxy in order to harvest a meaningful description and re-throw for the upper tiers can handle it as they see fit.
JohnIdol
@JohnIdol: I am not saying, that you should have only a single handler, but wrapping every call in try/catch is not the right thing to do.
Brian Rasmussen
looking for alternatives here :)
JohnIdol
A: 

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.

EFrank
+1  A: 

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

slugster
that's exactly what I am trying to do, harvesting inner descriptions and packaging in my own exceptions for the upper layers - I am looking for a way to do it that doesn't involve wrapping all the calls to that given class with a try catch block.
JohnIdol
+4  A: 

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.

Johann Blais
Thanks for your input. Yes, it's about WCF faults. I am using a service proxy that decouples the rest of the app from the service data contract. I basically need to extract fault exceptions specific fields (inner descriptions etc.), package them up into an custom exception and throw it again. I should probably update the question to reflect all this. I was trying to approach the problem from a generic perspective. :)
JohnIdol
@Johnldol, I'm not sure if this is what you're describing, but a typical pattern with WCF is to wrap all usage of the proxy via a method that takes a delegate for the action to perform on the service. This method can open the channel, catch any exceptions thrown during usage of the service (such as communication faults) and transform them appropriately, then close the channel. This is also where you guarantee that you call Abort() on the channel if it has faulted.
Dan Bryant
@Dan I have something like that in the service proxy base class for communication errors. The issue here is that each implementation of the service proxy for a specific service needs to handle specific faults (different types declared by the given service being 'proxied') and extract whatever description fields are on the fault classes.
JohnIdol
A: 

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

Archeg