views:

312

answers:

7

Is it possible to catch exceptions in a single place in a c# class file?

I'm coding some unit tests in NUnit to test for a WCF Web-Service, and on all methods/tests want to trap a "EndpointNotFoundException" without having to code this for each test.

edit

I guess i wanted to create a descriptive error in this case without having to put an additional catch block in each method, as i do indeed want the test to fail.

As i've done something similar to this in WCF with the FaultException i was interested to know if it was possible in general with C# classes

But the bottom line is that if it fails, it fails! thanks to @TrueWill for stating the obvious ;) and to @Abhijeet Patel for making me think more about how i structure my unit tests

(oh, and apologies for answering my own question ;)

+2  A: 

Perhaps a better approach would be to look into injecting the exception handling code with AOP (postsharp) or policy injection?

AFAIK, what you are trying to do is not possible. You can look into Application.ThreadException and AppDomain.CurrentDomain.UnhandledException for centralized exception handling.

Joepro
A: 

You'll have to create a logging mechanism (or use a third party implementation like log4net) and insert catch blocks everywhere, where the exceptions are logged. In other words, no.

Chris
+1  A: 

Aspect oriented programming can be done to handle a single type of exception like EndpointNotFoundException..and the exception can be logged somewhere using that..

Jaswant Agarwal
+3  A: 

In general, no - you can only catch locally. There are isolated exceptions occasions when you can do this - ASP.NET MVC controllers and WCF services being two examples that leap to mind where the error handling can easily (or easily enough) separated.

But in your case - don't you just want to add [ExpectedException(...)] to the affected tests?

Marc Gravell
TrueWill
A: 

As Marc already pointed out, using an [ExpectedException] on the unit test seems to be the most logical way to go especially for unit tests, where you want to focus on a unit of functionality. if you are doing a lot of try...catch style processing and conditional checking then your unit test isn't really a unit test

Abhijeet Patel
+1  A: 

You can use AOP to achieve this. The idea is, as you request, to attach some behaviour (exception handling in this case) to all methods in the class.

For example, using PostSharp, you can define the following "exception handler":

[Serializable]
class EndpointNotFoundExceptionHandlerAspect : OnExceptionAspect
{
    public override void OnException(MethodExecutionEventArgs eventArgs)
    {
        if (eventArgs.Exception is EndpointNotFoundException)
            eventArgs.FlowBehavior = FlowBehavior.Continue; // continue without throwing an exception
        else
            base.OnException(eventArgs);
    }
}

Then add the EndpointNotFoundExceptionHandlerAspect to your class definition. Then every time the EndpointNotFoundException is thrown, it will be "handled".

Note: I make no claims that this is a good idea. This is merely an example of how it can be achieved.

Nader Shirazie
A: 

I guess i wanted to create a descriptive error in this case without having to put an additional catch block in each method, as i do indeed want the test to fail.

As i've done something similar to this in WCF with the FaultException i was interested to know if it was possible in general with C# classes

But the bottom line is that if it fails, it fails! thanks to @TrueWill for stating the obvious ;) and to @Abhijeet Patel for making me think more about how i structure my unit tests

youthinkthisisntme
Please edit your question. You have just added an "answer", which is probably not what you had in mind.
John Saunders