Try/catch is not working in AX 2012 / D365
Views (382)
Sometimes we see catch statement does not catch the errors even though the code looks good.
I was getting some errors while trying to create customers using below code. The code looks good for me. But, still not working.
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.
I was getting some errors while trying to create customers using below code. The code looks good for me. But, still not working.
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 This was originally posted here.

Like
Report
*This post is locked for comments