Handle exceptions in your code
Applies To: Dynamics CRM 2013
There are a number of exceptions that can be returned from a Microsoft Dynamics CRM Web service method call. Your application design must catch and appropriately handle these exceptions. In the Microsoft Dynamics CRM SDK, 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.
Common exceptions and faults
The following code is used in most Microsoft Dynamics CRM SDK samples. It highlights the common faults and exceptions that your application design should handle.
catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
{
Console.WriteLine("The application terminated with an error.");
Console.WriteLine("Timestamp: {0}", ex.Detail.Timestamp);
Console.WriteLine("Code: {0}", ex.Detail.ErrorCode);
Console.WriteLine("Message: {0}", ex.Detail.Message);
Console.WriteLine("Inner Fault: {0}",
null == ex.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault");
}
catch (System.TimeoutException ex)
{
Console.WriteLine("The application terminated with an error.");
Console.WriteLine("Message: {0}", ex.Message);
Console.WriteLine("Stack Trace: {0}", ex.StackTrace);
Console.WriteLine("Inner Fault: {0}",
null == ex.InnerException.Message ? "No Inner Fault" : ex.InnerException.Message);
}
catch (System.Exception ex)
{
Console.WriteLine("The application terminated with an error.");
Console.WriteLine(ex.Message);
// Display the details of the inner exception.
if (ex.InnerException != null)
{
Console.WriteLine(ex.InnerException.Message);
FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> fe = ex.InnerException
as FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>;
if (fe != null)
{
Console.WriteLine("Timestamp: {0}", fe.Detail.Timestamp);
Console.WriteLine("Code: {0}", fe.Detail.ErrorCode);
Console.WriteLine("Message: {0}", fe.Detail.Message);
Console.WriteLine("Trace: {0}", fe.Detail.TraceText);
Console.WriteLine("Inner Fault: {0}",
null == fe.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault");
}
}
}
Note
If you are accessing the discovery Web service, your code should catch DiscoveryServiceFault instead of the OrganizationServiceFault fault shown above.
In addition to these exceptions and faults, your code must handle the following exceptions:
When connecting to Microsoft Dynamics CRM Online, a SecurityAccessDeniedException exception can be thrown if you use a valid Microsoft account and your Live account is not associated with any Microsoft Dynamics CRM Online organization. A MessageSecurityException can be thrown if your Microsoft account is not valid or Windows Live failed to authenticate you.
Additional information about exceptions
When an uncaught exception is thrown that contains sensitive information that the user doesn’t have permission to see, the sensitive information in the exception is hidden from the user and a reference number is provided. This reference number refers to the related server event log entry and server trace entry. A system administrator can look up those entries and find more information about the exception.
See Also
Troubleshooting and error handling
Troubleshooting tips
Web service error codes
Handle exceptions in plug-ins
.NET Framework Developer Center