.NET Exceptions Vs WCF Faults

By | May 31, 2012

Exception is a .NET mechanism used to communicate problems encountered during the execution of a program. Various .NET languages allows us to throw, catch/handle or possibly ignore exceptions so that it can be further propagated up to the call stack.

C# sample code for handling exception in .NET applications below explains it as:
 int x = 0;
 int result = 0;
try

 {
            result = 100 / x; //this line will generate divide by zero exception
            //Some code here…
}

 catch (DivideByZeroException dbzex)
 {
           Console.WriteLine(dbzex.ToString());
 }
 catch (Exception ex)
 {
           Console.WriteLine(ex.ToString());
 }

While WCF Faults refer to SOAP fault mechanism that transfers errors or fault condition from service to consumer according to SOAP Specifications. If some exception occurs at WCF Service level (which is basically a CLR exception), throwing it as is to client is illogical because client of this service is not necessarily a .NET application. It might be a Java client or something else that might not be able to understand CLR exception.  Another important reason for not throwing CLR exceptions to client is, it contains internal details of our service code and off course we don’t want to expose those internal details as is to client.WCF Fault

In Windows Communication Foundation, we handle CLR exceptions and return error details to client using Fault Contract. A fault contract in WCF contains details of possible exceptions that might occur in a service code. Another WCF Tutorial with code sample, explains in detail about WCF Fault Contract here.

Following code samples will definitely help to understand WCF Faults.

 [ServiceContract]
 public interface IMyService1
 {
           [OperationContract]
           [FaultContract(typeof(MyWCFFault))]
           int MyOperation1();
 }

[DataContract]

 public class MyWCFFault
 {
         [DataMember]
         public string ErrorDetails { get; set; }
 }

Implementation code for WCF Service is:

 public int MyOperation1()
 {
         Try{
//Do something……
}
catch()

         {
             MyWCFFault ex = new MyWCFFault();
            ex.ErrorDetails = “Specific error details here.“;
            throw new FaultException(ex,“Reason: Testing…..“);
         }
 }

Top 10 Interview Questions and Answers Series:

Category: Uncategorized Tags: , ,

About IMRAN ABDUL GHANI

Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET (MCSD.NET) since 2005. You can reach his blogging at www.webdevelopmenthelp.net, www.topwcftutorials.net, and www.sharepointfordummies.net.

2 thoughts on “.NET Exceptions Vs WCF Faults

  1. Dieter Cote

    It is a stateless, system individual, XML based typical compact technique that uses HTTP as its transport technique and can be used for developing assigned complex handling environment.

  2. sarabjeet

    Thanks for all the information,it was very helpful and i really like that you are providing information on .net training ,being enrolled in .net freshers training with projects live training http://www.wiziq.com/course/57-fresher-training-projects i was looking for such .net fresher training to assist me and your information helped me a lot.Really like that you are providing such information . Thanks.

Comments are closed.