Video of the article is HERE
This article will give basic introduction on
1. How to handle Exception at service side?
2. How to use Fault contract at Service side?
3. How to handle Service Exception at client side?
Video explaining this article is here
Few points about Exception at Service
1. Exception is technology specific.
2. Exception should not share beyond service boundary.
3. Since Exception is technology specific they cannot be propagated to client.
4. Exception are of types
Ø CLR Exception
Ø Windows32 Exception
Ø Runtime Exception at service
Ø C++ Exception
5. Exception are very much native to the technology in which service is made.
6. Exception must be converted from technology specific information to natural information that can be communicated to client.
1. Service should throw FaultException<T> instead of usual CLR exception.
2. FaultException<T> is specialization of Fault Exception.
3. Any client that programs against FaultException can handle exception thrown by FaultException<T>.
4. The type parameter T conveys the error detail.
5. T can be of any type like Exception, CLR Type or any type that can be serialized.
6. T can be of type Data contract.
7. T is a generic parameter conveys the error details.
About Fault Contract
1. Any Exception service want to share beyond service boundary comes must be goes at the part of service contract.
2. Fault contract says what type of exception service can throw to client.
3. The type in Fault Contract must be the same as the T of FaultException<T>
4. Fault contract is the contact between service and client about what exception can be thrown from service to client.
5. Fault contract is attributing of a method in the service. So only attributed method can throw the type of exception specified in Fault Contract.
1. Create a simple WCF service with only one method. This method will have one output and two input parameters.
2. Implement the service contract
3. Press F5 to host the service in cassini server. Copy the URL from here.
4. Create a console client and add the service reference of the service we created.
5. Handle the exception at client side
6. Press F5 to get the exception information at client side. On running , you will get the exception message as below
So, we are getting the proper error message at the client side.
For your reference, source code is as below
public interface IService1
double divide(int num1, int num2);
public class Service1 : IService1
public double divide(int num1, int num2)
if (num2 == 0)
DivideByZeroException myException = new DivideByZeroException();
throw new FaultException<DivideByZeroException>(myException, “NUMBER 2 is 0”);
return num1 % num2;
static void Main(string args)
Service1Client proxy = new Service1Client();
double result = proxy.divide(30,0);
catch (FaultException<DivideByZeroException> ex)
Thanks for reading. I hope this article was useful for you. Happy Coding.
Leave a Reply