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.
- public int Add(int number1, int number2)
- {
- return number1 + number2;
- }
- public double Divide(int number1, int number2)
- {
- return number1 / number2;
- }
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.
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.
- public double Divide(int number1, int number2)
- {
- try
- {
- return number1 / number2;
- }
- catch (DivideByZeroException exception)
- {
- throw exception;
- }
- }
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.



Anu VPosted Mar 27, 2017, 2:03 AM
Nice article.. Thanks for sharing..