What is a Fault Exception in WCF and why do we use it? This article explains the need for a Fault Exception.
Fault exception in WCF
It is used in a client application to catch contractually-specified SOAP faults. By the simple exception message, you can't identify the reason of the exception, that's why a Fault Exception is useful.
In this article I will:
- Create a WCF service application that has an operation contract for the division of 2 passed values by the client.
- Create a Console application and add a reference for the WCF service into it. Then access the method of the WCF service to do the division of passed values.
To understand it in details try the following example:
Step 1
- Create a WCF Service Application named "FaultException".

- Write an operation contract in the interface of the default file named "IService1.cs".
- [OperationContract]
- int Divison(int val1, int val2);

- Write the following code in "Service1.svc.cs" for implementation of an operation contract for the division of 2 values with a try and catch block.
- public int Divison(int val1, int val2)
- {
- try
- {
- return val1 / val2;
- }
- catch (Exception ex)
- {
- throw ex; // throw the exception if raises
- }
- }

- Run the WCF service and note down the URL like "http://localhost:3857/Service1.svc" somewhere.









Khushboo ModiPosted Feb 18, 2017, 2:01 AM
NOt able to handle fault exception why ?Try { string uri = "http://localhost:62598/Service1.svc/Divide"; HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest; request.Method = "GET"; using (WebResponse responce = request.GetResponse()) { } } catch (FaultException<string> ex) { apiResponce = ex.Message.ToString(); }