August 10, 2011 at 11:57
filed under Dynamics AX
Tagged Dynamics AX, Exceptions, Try Catch
Hi all,
Exception handling can be quite confusing in Dynamics AX, so I wanted to share some quick facts about try/catch and transactions.
The general rule is that exceptions are caught in the outer most catch, where the ttslevel is 0. This means that if you put a transaction around a try/catch, your exceptions will not be caught.
The following two jobs demonstrate that:
Transaction inside try/catch:
Output:
Error an error
Info error caught
Try/catch inside transaction:
Output:
Error an error
As you can see, the error is not caught when the transaction is around the try catch.
However, there are two exceptions to this rule.
The following code demonstrates that UpdateConflict and DupplicateKeyException can be caught inside a transaction.
Output:
Info Info can only be caught outside of the transaction
Info Warning can only be caught outside of the transaction
Info Deadlock can only be caught outside of the transaction
Info Error can only be caught outside of the transaction
Info Internal can only be caught outside of the transaction
Info Break can only be caught outside of the transaction
Info DDEerror can only be caught outside of the transaction
Info Sequence can only be caught outside of the transaction
Info Numeric can only be caught outside of the transaction
Info CLRError can only be caught outside of the transaction
Info CodeAccessSecurity can only be caught outside of the transaction
Warning UpdateConflict can be caught inside transaction
Warning UpdateConflict can also be caught outside of the transaction
Info UpdateConflictNotRecovered can only be caught outside of the transaction
Warning DuplicateKeyException can be caught inside transaction
Warning DuplicateKeyException can also be caught outside of the transaction
Info DuplicateKeyExceptionNotRecovered can only be caught outside of the transaction
Info Timeout can only be caught outside of the transaction
Info PassClrObjectAcrossTiers can only be caught outside of the transaction
It is also noteworthy that, when you enter a catch block, there has been a implicit ttsabort, so the transaction has been rolled back. However this is not true for UpdateConflict and DuplicateKeyException when they were caught inside a transaction.
AlexOnDAX
on January 22, 2016 at 20:51
Great post. One change I made to your demo code to make it a little more obvious is I changed the 3 info/warning messages to have the TTSLevel:
warning(strFmt(“%1 can be caught inside transaction (TTSLevel: %2)”, exception, appl.ttsLevel()));
warning(strFmt(“%1 can also be caught outside of the transaction (TTSLevel: %2)”, exception, appl.ttsLevel()));
info(strFmt(“%1 can only be caught outside of the transaction (TTSLevel: %2)”, exception, appl.ttsLevel()));
Trackbacks
on October 4, 2023 at 08:54