Ali Ersöz
Ali Ersöz

Reputation: 16086

Cannot handle FaultException

i have a wcf service that does an operation. and in this operation there could be a fault. i have stated that there could be a fault in my service contract.

here is the code below;

public void Foo()
{
        try
        {
            DoSomething(); // throws FaultException<FooFault>
        }
        catch (FaultException)
        {
            throw;
        }
        catch (Exception ex)
        {
            myProject.Exception.Throw<FooFault>(ex);
        }
}

in service contract;

[FaultException(typeof(FooFault))]
void Foo();

when a FaultException was thrown by DoSomething() method while i was running the application, firstly the exception was caught at "catch(Exception ex)" line and breaks in there. then when i pressed f5 again, it does what normally it has to. i wonder why that break exists? and if not could it be problem on publish?

Upvotes: 1

Views: 5395

Answers (5)

Konamiman
Konamiman

Reputation: 50273

Are you consuming the WCF service from Silverlight? If so, a special configuration is needed to make the service return a HTTP 200 code instead of 500 in case of error. The details are here: http://msdn.microsoft.com/en-us/library/dd470096%28VS.96%29.aspx

Upvotes: 2

Jonathan C Dickinson
Jonathan C Dickinson

Reputation: 7275

The problem is that exceptions are checked in the order they are declared. Try putting the Exception catch block first and you will see that the compiler complains: other catch blocks will NEVER be evaluated. The following code is generally what .Net is doing in your case:

        // Begin try
             DoSomething(); // throws FaultException<FooFault>
        // End try
        if (exceptionOccured)
        {
            if(exception is FaultException) // FE catch block.
            {
                throw;
                // Goto Exit
            }
            if(exception is Exception) // EX catch block
            {
                throw new FaultException<FooFault>();
                // Goto Exit
            }
        }

        // Exit

As you can see your FaultException never re-enters the try-catch-finally (i.e. try-catch-finally is not recursive in nature).

Try this instead:

        try
        {
            try
            {
                DoSomething(); // throws FaultException<FooFault>
            }
            catch (Exception ex)
            {
                if (ex is FaultException<FooFault>)
                    throw;
                else
                    myProject.Exception.Throw<FooFault>(ex);
            }
        }
        catch (FaultException)
        {
            throw;
        }

HTH.

Upvotes: -1

Serhat Ozgel
Serhat Ozgel

Reputation: 23766

Actually your exception is caught but you fail to notice it since visual studio highlights the next line, not the line throwing the exception. Replace

throw;

with some other lines and see them in action.

Upvotes: 1

aku
aku

Reputation: 123956

@yapiskan,

C# is a strong typed language Foo< X> != Foo. So if you need to catch some exception, provide exact type in catch clause.

You can learn more on exception handling reading this MSDN article.

Upvotes: -1

aku
aku

Reputation: 123956

Take a closer look at catched exception. Was it FaultException< FooFault> or FaultException ? There are 2 version of FaultException class: generic and non-generic

Upvotes: 0

Related Questions