1

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?

Ali Ersöz
  • 15,860
  • 11
  • 50
  • 64

5 Answers5

2

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

Konamiman
  • 49,681
  • 17
  • 108
  • 138
1

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.

Serhat Ozgel
  • 23,496
  • 29
  • 102
  • 138
  • This is not working. Becuase the throw line is not in the try block so it has to be caught by the parent catch block. – Ali Ersöz Nov 13 '08 at 13:55
0

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

aku
  • 122,288
  • 32
  • 173
  • 203
  • maybe *you* should have a closer look ;) [SerializableAttribute] public class FaultException : FaultException var fooFault = new FaultException(); fooFault is FaultException;//is true so catch block with catch (FaultException ex) will catch FaultException *just saw your comments above... this comment is still misleading – RhysC Dec 22 '09 at 03:08
-1

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.

Jonathan C Dickinson
  • 7,181
  • 4
  • 35
  • 46
  • I knew that the catch blocks are not recursive, but using catch blocks as you mentioned is going to make the code like stairs and I don't like it. And I am not at the point of why the exception is not caught. It is caught but there exists a break in there and forces me to push f5 button to continue. – Ali Ersöz Nov 17 '08 at 09:51
-1

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

aku
  • 122,288
  • 32
  • 173
  • 203