Fault and Exception Handling in Dynamics CRM 2011

By | August 2, 2011
Microsoft Dynamics CRM Web service can throw a number of exceptions. When we use Microsoft Dynamics CRM Web service in application development we need to handle service related exceptions. All Web service method calls use a communication channel to the server based on the Windows Communication Foundation (WCF) technology. In WCF terms, exceptions returned from the channel are called faults.
Here we have listed some of the common exceptions that one should handle in the code:
DiscoveryServiceFault: If you are accessing the discovery Web service, you need to catch DiscoveryServiceFault exception.(See below code)
catch (FaultException< Microsoft.Xrm.Sdk.DiscoveryServiceFault> ex)
{
Console.WriteLine(“The application terminated with an error.”);
}
OrganizationServiceFault: If you are accessing the organization Web service, you need to catch OrganizationServiceFault exception.(See below code)

catch (FaultException ex)
{
Console.WriteLine(“The application terminated with an error.”);
}
TimeoutException: This will occurred when take long time to execute CRM request.(See below code)
catch (System.TimeoutException ex)
{
Console.WriteLine(“The application terminated with an error.”);
}
SecurityAccessDeniedException: When connecting to Microsoft Dynamics CRM Online, SecurityAccessDeniedException exception can be thrown if you use a valid Windows Live ID and your Live account is not associated with any Microsoft Dynamics CRM Online organization.(See below code)
catch (FaultException ex)
{
Console.WriteLine(“The application terminated with an error.”);
}
MessageSecurityException: A MessageSecurityException can be thrown if your Windows Live ID is not valid or Windows Live failed to authenticate you.
catch (FaultException ex)
{
Console.WriteLine(“The application terminated with an error.”);
}