FaultException<T>() exception thrown by the service is not caught by the client catch(FaultException

Posted by Ashish Gupta on Stack Overflow See other posts from Stack Overflow or by Ashish Gupta
Published on 2010-06-13T13:00:05Z Indexed on 2010/06/13 13:22 UTC
Read the original article Hit count: 976

Ok, I know I am missing something here. I have the following operation contract:

public double DivideByZero(int x, int y)
{                   
    if (y == 0) 
    { 
        throw new FaultException<ArgumentException>
          (new ArgumentException("Just some dummy exception")
          ,new FaultReason("some very bogus reason"), new FaultCode("007"));
    }
    return x / y;
}

And following is taken from the client:-

  Console.WriteLine("Enter the x value");
  string x = Console.ReadLine();
  Console.WriteLine("Enter the Y value");
  string y = Console.ReadLine();
  try
  {
      double val = client.DivideByZero(Convert.ToInt32(x), Convert.ToInt32(y));
      Console.WriteLine("The result is " + val.ToString());
  }
  catch(FaultException<ArgumentException> exp)  
  {
      Console.WriteLine("An ArgumentException was thrown by the service "+ exp.ToString());    
  }
  catch (Exception exp)
  {
      Console.WriteLine(exp.ToString());
  }

In the above case catch(FaultException exp) (the first catch block with ArgumentException in the client code) block does not get executed. However, when I remove ArgumentException to have catch(FaultException exp), the same catch block gets executed. I am not sure about this as I am throwing FaultException from my operation contract. Am I missing anything here.

Appreciate your help,

Ashish

© Stack Overflow or respective owner

Related posts about c#

Related posts about wcf