Monday, 18 November 2019

Try/catch is not working in AX 2012 / D365

Sometimes we see catch statement does not catch the errors even though the code looks good.

The below code looks good for me. But, still not showing my custom exceptions.

while (resultSet.next())
{
    //some logic (...)

    ttsbegin;

    //This is a custom table that stores to where I want to integrate the given customer
    while select integrationCompany
            where integrationCompany.CRMCompany == customerInfo.parmCRMDataAreaId()
    {
        changeCompany(integrationCompany.ERPCompany)
        {
            try
            {
                customerInfo.createCustomer();

                //.. some more logic


            }
            catch
            {
                // My catch Block, that should update the source database to set
                // the processing status to "error"


                ttsAbort;
            }
        }
    }

    ttsCommit;

}



Reason: 
Because the try and catch statement is inside the transaction(ttsbegin and ttscommit) and this is what causing the issue.

Solution:

ttsbegin and ttscommit should be always inside the try and catch statements as shown below.



while (resultSet.next())
{
    //some logic (...)

    //This is a custom table that stores to where I want to integrate the given customer
    while select integrationCompany
            where integrationCompany.CRMCompany == customerInfo.parmCRMDataAreaId()
    {
        changeCompany(integrationCompany.ERPCompany)
        {
            try
            {
               ttsbegin;
              customerInfo.createCustomer();

                //.. some more logic
              ttscommit


            }
            catch
            {
                // My catch Block, that should update the source database to set
                // the processing status to "error";
               ttsbegin;
               //you update statement here
               ttscommit;
            }
        }
    }

}

For more details please refer : https://stackoverflow.com/questions/27529308/strange-behavior-with-try-catch-on-dynamics-ax-2012  

3 comments:

  1. Nice blog. You have provided such a useful informartion in this blog. Thanks for sharing.
    MS Dynamics Operation Trade and Logistics Training

    ReplyDelete
  2. I am impressed. I don't think Ive met anyone who knows as much about this subject as you do. You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here. https://catcherrors.com/repos/pandas-dev/pandas

    ReplyDelete
  3. Thank you so much, I was having hard time figuring it out why my code was not able to catch exceptions

    ReplyDelete

Adding a newline into a string in C# and X++

Below is the sample code we can use for  adding a newline after every occurrence of "@" symbol in the string in C#   using System...