Handle Exceptions in Your Code
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:
-
SecurityTokenValidationException
-
ExpiredSecurityTokenException
-
SecurityAccessDeniedException
-
MessageSecurityException
-
SecurityNegotiationException
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.
Important |
|---|
| If your organization has been updated to Microsoft Dynamics CRM Online Fall ’13, please use the latest version of the SDK. Download the updated SDK package for Microsoft Dynamics CRM 2013 and CRM Online Fall ‘13. |
See Also
Microsoft Dynamics CRM 2011
Send comments about this topic to Microsoft.
© 2013 Microsoft Corporation. All rights reserved.
Note