Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

axapta - How to catch inner exception?

Is there any possibility to catch inner exception:

try 
{
    ttsbegin;    
    info("step one");        
    try 
    {
       info("step two");
       throw Error("error");
    }
    catch 
    {
       info("catch step two");
    }        
    ttscommit;
}
catch 
{
    info("catch step one");
    ttsabort;
}

I know I can get it commenting ttsbegin; / ttscommit, but I need to have a transaction.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No, it is not possible (unless your exception is UpdateConflict or DuplicateKeyException).

The documentation states:

If an exception is thrown inside a transaction, the transaction is automatically aborted (a ttsAbort operation occurs). This applies both for exceptions thrown manually and for exceptions thrown by the system.

When an exception is thrown inside a ttsBegin - ttsCommit transaction block, no catch statement inside that transaction block can process the exception. Instead, the innermost catch statements that are outside the transaction block are the first catch statements to be tested.

The logic is: 1) your transaction is aborted by the throw 2) then you cannot possible recover from that inside your transaction 3) hence take the innermost catch outside the transaction.

The two exceptions (pun intended) are UpdateConflict and DuplicateKeyException which do not make a ttsabort and hence may be caught inside the transaction.

Also see this blog entry which demonstrate that.

Update: Potential pitfall

Using catch all (no exception type specified) can cause problems. See this blog post.
As of D365O update 5 the the two exceptions are not caught by a catch all if the tts level is greater than one. See this blog post.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...