.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:

  • http://dawnrocha.wordpress.com/2013/04/12/the-net-development-framework-for-software-development-provides-very-efficient-platform-for-software-developers/ 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.

  • http://www.blogger.com/profile/11223974173581186160 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.