latest Post

Custom Exceptions in CRM Sandbox Plugins (Unit Test Samples)

Recently we have started to utlise the "FakeXRMEasy" library in order to perform unit testing on any of  our MS Dynamics plugins.  
While creating a new plugin recently I wanted to test that exceptions were thrown when various values were not passed in. 

To do this with the FakeXRMEasy Library we just need to add the  "Expected Exception" attribute to the method, see below. 

Screenshot showing expected general "InvalidPluginExecutionException"

However, I wanted to ensure the correct exception was thrown each time rather than just an generic "InvalidPluginExecutionException" therefore I decided to create my own custom exceptions which inherited from the OOB "Exception" class. This way in each of my tests I could decorate each method with the correct expected exception.

Screenshot showing expected custom exception "UserCannotEditRecordException"

This worked perfectly and all my unit tests passed however, when deploying the plugin and running within the Dynamics web interface I got the below unexpected error instead of the custom exception message I expected.  

What appears to be happening is  the correct exception is thrown however, CRM doesn't know how to handle it so a further unexpected exception is thrown.

I then tried inherting from the "InvalidPluginExecutionException" class instead thinking CRM knows about this so it should work however, I am prevented from inhering from this as it was created as a sealed class.  

After a little bit more research it came apparent that Custom exceptions are not supported in Dynamics CRM Sandboxed Plugins something I was never aware of having never needed to do this in the past. 

For those interested to ensure the correct exceptions were hit in my unit tests rather than using the expected exception attribute I simply wrapped a try catch round the test and asserted that the exception message contained the text I expected. 

Recommended Posts × +


  1. "We can easily redesign your existing website making it up to the calibre of your brand so that you can easily retain your customers; climbing the top position.
    Address: Asia Pacific Trade Center, Rashid Minhas Rd, Karachi
    Phone: 0316 2586964" Codeaze

  2. That is very interesting; you are a very skilled blogger. I have shared your website in my social networks! A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article.
    CRM Software
    Business Management Software

  3. I really want to thank Dr Emu for saving my marriage. My wife really treated me badly and left home for almost 3 month this got me sick and confused. Then I told my friend about how my wife has changed towards me. Then she told me to contact Dr Emu that he will help me bring back my wife and change her back to a good woman. I never believed in all this but I gave it a try. Dr Emu casted a spell of return of love on her, and my wife came back home for forgiveness and today we are happy again. If you are going through any relationship stress or you want back your Ex or Divorce husband you can contact his whatsapp +2347012841542 or email website: Https:// Https://