AXForum  
Вернуться   AXForum > Microsoft Dynamics AX > DAX Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск Все разделы прочитаны

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 29.10.2012, 19:11   #1  
Blog bot is offline
Blog bot
Участник
 
25,607 / 848 (80) +++++++
Регистрация: 28.10.2006
ax-erp: Try Catch and transactions
Источник: http://microsoft-dynamics-ax-erp.blo...nsactions.html
==============


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:
try
{
ttsBegin;
throw error("an error");
ttsCommit;
}
catch
{
info("error caught");
}

Output:
Error an error
Info error caught
Try/catch inside transaction:
ttsBegin;
try
{
throw error("an error");
}
catch
{
info("error caught");
}
ttsCommit;

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.

Set set = new Set(Types::Enum); // a set containing all possible exceptions
SetEnumerator se; // enumerator used to loop though the set with exception
Exception exception; // used to cast the value from the set
boolean caughtInside;
;

// add all exception to a set
set.add(Exception::Break);
set.add(Exception::CLRError);
set.add(Exception::CodeAccessSecurity);
set.add(Exception::DDEerror);
set.add(Exception::Deadlock);
set.add(Exception::DuplicateKeyException);
set.add(Exception::DuplicateKeyExceptionNotRecovered);
set.add(Exception::Error);
set.add(Exception::Info);
set.add(Exception::Internal);
set.add(Exception::Numeric);
set.add(Exception::PassClrObjectAcrossTiers);
set.add(Exception::Sequence);
set.add(Exception::Timeout);
set.add(Exception::UpdateConflict);
set.add(Exception::UpdateConflictNotRecovered);
set.add(Exception::Warning);

// create enumerator
se = set.getEnumerator();

// loop all exceptions
while(se.moveNext())
{
// set flag false
caughtInside = false;
// begin outer try catch
try
{
ttsBegin;
// begin inner try catch
try
{
// cast exception
exception = se.current();
// trhow exception
throw exception;
}
catch
{
// set flag to indicate the exception was caught inside the transaction
caughtInside = true;

warning(strFmt("%1 can be caught inside transaction", exception));
// throw exception again to catch it outside of transaction
throw exception;
}
ttsCommit;
}
catch
{
// for once, it's ok to catch everyting :)

if(caughtInside)
{
warning(strFmt("%1 can alse be caught outside of the transaction", exception));
}
else
{
info(strFmt("%1 can only be caught outside of the transaction", exception));
}

}
}

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 alse 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 alse 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.



Источник: http://microsoft-dynamics-ax-erp.blo...nsactions.html
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
emeadaxsupport: New Content for Microsoft Dynamics AX 2012 : October 2011 Blog bot DAX Blogs 0 27.10.2011 17:11
axzaptech: Exceptions Inside Transactions Blog bot DAX Blogs 0 28.01.2011 23:14
Issues concerning X++: Exception handling and transactions Blog bot DAX Blogs 1 14.02.2009 00:46
Holzheys: X++ Transactions, exceptions and updateconflicts. Blog bot DAX Blogs 2 10.10.2008 01:23
staticsax: New approach to Exceptions and Transactions handling Blog bot DAX Blogs 0 13.11.2007 20:30
Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 23:21.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.