locked
Try Catch not working in CRM 2011 online plugin RRS feed

  • Question

  • Hi

    In order to try and debug a plugin for crm 2011 online I have created en error message entity where the error will be written to. I do get the custom error message inside crm but the try catch is noget running.

    try{

     

    }catch(Exception ex){

     

    }

    Have also tried with InvalidPluginExecutionException and FaultException. The plugin is set to run on the create account event. Any suggestions?

    Wednesday, May 25, 2011 9:36 AM

Answers

  • The only errors that are actually catched are of type InvalidPluginExecutionException, so you need to rethrow your exception, like this:

    try{
    
     
    
    }catch(Exception ex){
    
     
    throw new InvalidPluginExecutionException(ex);
    }
    

    Wednesday, May 25, 2011 9:43 AM
  • I am also going to guess that the error doesn't occur in side your try catch.  If you had a local dev environment I would suggest deploying the plugin locally after registering on disk with the .PDB file deployed also.  Then you can attach visual studio to the w3wp service on the server of the sandbox worker service depending on how you register, set a break point, execute your plugin in CRM, and literally debug your code line by line like it was local.
    Jamie Miley
    http://mileyja.blogspot.com
    Linked-In Profile
    Follow Me on Twitter!
    Wednesday, May 25, 2011 1:19 PM
    Moderator
  • Use trace or write a unit test with Rhino Mocks. You don't need to deploy to debug:)
    Wednesday, May 25, 2011 2:26 PM

All replies

  • The only errors that are actually catched are of type InvalidPluginExecutionException, so you need to rethrow your exception, like this:

    try{
    
     
    
    }catch(Exception ex){
    
     
    throw new InvalidPluginExecutionException(ex);
    }
    

    Wednesday, May 25, 2011 9:43 AM
  • But it never gets into the catch section. I dont wanne display the error to the user.
    Wednesday, May 25, 2011 10:37 AM
  • Are you sure that the error gets raised within the try - section?
    Wednesday, May 25, 2011 10:39 AM
  • inside the catch im creating a new record of my custom pluginmessage entity. The same code works well outside the catch and therefore I think I never get inside the catch. The pluginmessage will hold information about the error that I then can use to debug.
    Wednesday, May 25, 2011 10:50 AM
  • The code inside the catch will only execute if an error is raised within the try. If have no need to catch any exception, you should not need to use a try-catch block

    To force the code within the catch block to run, you would need to put something like this in:

    try
    {
    
    throw new Exception("message);
    }
    catch (Exception ex)
    {
    //place your code here
    }
    
    But that would be completely redundant - you're better off removing the try-catch - block entirely

    Wednesday, May 25, 2011 10:55 AM
  • Even when there is an error it does not go inside the catch block. I do see the standard error message in the browser.
    Wednesday, May 25, 2011 12:30 PM
  • Can you post the entire code?
    Wednesday, May 25, 2011 12:37 PM
  • I am also going to guess that the error doesn't occur in side your try catch.  If you had a local dev environment I would suggest deploying the plugin locally after registering on disk with the .PDB file deployed also.  Then you can attach visual studio to the w3wp service on the server of the sandbox worker service depending on how you register, set a break point, execute your plugin in CRM, and literally debug your code line by line like it was local.
    Jamie Miley
    http://mileyja.blogspot.com
    Linked-In Profile
    Follow Me on Twitter!
    Wednesday, May 25, 2011 1:19 PM
    Moderator
  • I am also going to guess that the error doesn't occur in side your try catch.  If you had a local dev environment I would suggest deploying the plugin locally after registering on disk with the .PDB file deployed also.  Then you can attach visual studio to the w3wp service on the server of the sandbox worker service depending on how you register, set a break point, execute your plugin in CRM, and literally debug your code line by line like it was local.
    Jamie Miley
    http://mileyja.blogspot.com
    Linked-In Profile
    Follow Me on Twitter!
    Wednesday, May 25, 2011 1:19 PM
    Moderator
  • Thank you Jamie but I know all about how to debug using VS as I have done that for the past 4 year now :-)

    But on CRM 2011 online I can't debug and I can't download the solution as is it managed and so I have to try the try catch way. I know for a fact that there is an error inside my try as I have one to test the catch. I do see the error in the browser but the plugin stops at the error and does not move to the catch block.

    Wednesday, May 25, 2011 1:44 PM
  • Use trace or write a unit test with Rhino Mocks. You don't need to deploy to debug:)
    Wednesday, May 25, 2011 2:26 PM
  • Tuesday, February 14, 2012 6:20 PM
    Moderator