Why Normal .NET Exception Doesn't Work In WCF

Errors and exceptions are part of our programming life and WCF is no different. Thus, when we get errors in WCF, we would like to propagate those errors to our WCF clients, so that they can accordingly take action. In order to demonstrate this, let’s go through the code of a simple Service given below.
  1. public int Add(int number1, int number2)
  2. {
  3. return number1 + number2;
  4. }
  5. public double Divide(int number1, int number2)
  6. {
  7. return number1 / number2;
  8. }
Both the methods given above will perform some calculation and return the result to the client. Let’s say, now for some reason that someone sent 2nd parameter of Divide method as 0.

What will happen? The code will definitely throw an error or say DivideByZero exception. Won't it?

Know how to handle this error? Most of the developers will simply decorate Divide method with Try-catch block and throw the exception, which is similar to our normal .NET exception handling mechanism, as shown below.
  1. public double Divide(int number1, int number2)  
  2.         {  
  3.             try  
  4.             {  
  5.                 return number1 / number2;  
  6.             }  
  7.             catch (DivideByZeroException exception)  
  8.             {  
  9.                 throw exception;  
  10.             }  
  11.         }   
This normal exception handling mechanism will not work in WCF world. Now, before discussing why, let’s quickly see what is passed from the client.
  1. public double Divide(int number1, int number2)  
  2.         {  
  3.             try  
  4.             {  
  5.                 return number1 / number2;  
  6.             }  
  7.             catch (DivideByZeroException exception)  
  8.             {  
  9.                 throw exception;  
  10.             }  
  11.         }  
Now, if we will run this client Application, we will land upon an error message given below.



Now, by looking at the error message, we cannot figure out what went wrong as it only mentions it is an internal error :(

Now, coming back to the same question, why is it happening like this?

Well, the reason behind this is the message format being used by WCF. WCF uses, XML or XML SOAP to communicate with the clients. Hence, even if any exception is raised, it has to be in an XML format. Hence normal .NET exception handling mechanism doesn’t work here because the error is not sent to the clients in the form of an XML.
Thus, the solution here is Fault Exceptions and rather than throwing a normal .NET exception, we have to throw a fault exception, which is shown below.
  1. public double Divide(int number1, int number2)  
  2.         {  
  3.             try  
  4.             {  
  5.                 return number1 / number2;  
  6.             }  
  7.             catch (DivideByZeroException exception)  
  8.             {  
  9.                 throw new FaultException(exception.Message);  
  10.             }  
  11.         }  
Now, re-run our Application and we will be able to see proper error message.


On a summary note, we cannot use normal .NET exceptions to propagate exceptions to the client but the same can be used within WCF Service.

I hope you like this small but very useful tip. Happy learning.
Next Recommended Reading
Why WCF Web Service