We frequently receive questions regarding exceptions when writing plug-ins. How are exceptions handled? Should all inner exceptions be handled by the plug-in? Does Microsoft Dynamics CRM automatically log exceptions? What does an end user see when an exception goes unhandled? Fortunately these questions have fairly straightforward answers, as detailed in the following sections.
Exceptions and the Event Processing Pipeline
The impact of an unhandled exception within a plug-in on the event processing pipeline is fairly intuitive. If you registered your plug-in as a pre-event plug-in and it throws an exception or lets an exception go unhandled, no further plug-ins will execute and the core operation will not occur. If you registered your plug-in as a post-event and it throws an exception, no further plug-ins will execute, and since the core operation already occurred Microsoft Dynamics CRM will not roll it back. However, if the plug-in is executing in a child pipeline, an unhandled exception results in the parent pipeline’s core operation being rolled back.
Microsoft Dynamics CRM logs all unhandled exceptions in the Event Viewer on the server where they occurred. In addition, if the exception generating event was initiated by the user through the Microsoft Dynamics CRM user interface, the user is presented with an error message. To control the message that the user sees, you should throw an InvalidPluginExecution-Exception. In this case, the Message property for the exception is displayed. If you let an exception of another type go unhandled, a generic error message may be used.