Exception handling in WCF using SOAP FaultThe WCF library provide the FaultException class. You can find it in the System.ServiceModel namespace. If a WCF service thorws a FaultException object, the WCF runtime generates a SOAP Fault message that is sent back to the client application.
Here is the code to send Fault Exception:
try
{
// put your statements here..
}
catch(Exception ex)
{
throw new FaultException("Your Message comes here :" + ex.Message, new FaultCode("SenderName"));
}
So if an exception occurs, this code create a new FaultException object with the details of exception and thorw it back to the client application. Fault code provides a machine-readable identifier that can be used to programmatically identify the error condition and respond as appropriate. If you don't create a FaultCode object, the WCF runtime will automatically generate a FaultCode object.
Code to catch FaultException at Client application:
try
{
// call the WCF Service here..
}
catch(FaultException fe)
{
Response.Write(fe.Code.Name + ":" + fe.Reason);
}
So above function will display the fault code name and the Reason of exception that we have set at the time of creating FaultException object.
I hope it will help..
Happy Coding..