D365 Finance & Operations and Dynamics AX Forum

Expand all | Collapse all

development: try catch understanding

  • 1.  development: try catch understanding

    Posted 14 days ago

    Not sure if place is correct.. using standard try/catch instruction, when UpdateConflict is hit, when if condition won't be not true?
    Basicaly each interrupted try will:
    ->abort all opened ttsBegins and catch
    ->won't be ever executed when other tts is on higher level that current try(like class above in stack)

    So is anybody able to tell me when if (xSession::currentRetryCount() >= #RetryNum) won't be true?

        try
        {
            info(itemId);
        }
        catch (Exception::Deadlock)
        {
             retry;
        }
        catch (Exception::UpdateConflict)
        {
            if (appl.ttsLevel() == 0)
            {
                if (xSession::currentRetryCount() >= #RetryNum)
                {
                    throw Exception::UpdateConflictNotRecovered;
                }
                else
                {
                    retry;
                }
            }
            else
            {
                throw Exception::UpdateConflict;
            }
        }
    


    ------------------------------
    Hubert Piatek
    H&K International
    Dublin
    ------------------------------
    The first step toward cloud success. - Migrate from AX to D365 with expert guidance from Microsoft. I'm Ready


  • 2.  RE: development: try catch understanding

    TOP CONTRIBUTOR
    Posted 13 days ago
    X++ is a bit odd.
    You need a try/catch surrounding your tts blocks.
    An error condition will blow out of the tts block and start looking for the next try/catch.
    It was probably built this way to ensure bad data doesn't get into the system.
    A retry statement within a catch will retry from the top of the try block outside the transaction. I would expect your test of the retry count to be at the top of your try block, to ensure you don't end up in an infinite loop.

    Some good white papers from microsoft on the subject:
    https://docs.microsoft.com/en-us/dynamicsax-2012/developer/x-standards-try-catch-statements
    https://docs.microsoft.com/en-us/dynamicsax-2012/developer/exception-handling-with-try-and-catch-keywords
    https://docs.microsoft.com/en-us/dynamicsax-2012/developer/x-standards-ttsbegin-and-ttscommit

    ------------------------------
    Tony Zeigler
    Senior Consultant
    Strategic Solutions NW
    Beaverton OR
    ------------------------------

    The first step toward cloud success. - Migrate from AX to D365 with expert guidance from Microsoft. I'm Ready


  • 3.  RE: development: try catch understanding

    Posted 13 days ago
    Hi Tony,
    thank you for your post.
    I'm fully familiar with try catch in general however slightly concerned about else section of  catch (Exception::UpdateConflict)
    While TTS is already fully aborted leaving try segment and going to catch, how ttsLevel can be greater than 0 in case of update conflict spotted?
    please notice it's constructed like above in 80% cases of standard code

    ------------------------------
    Hubert Piatek
    H&K International
    Dublin
    ------------------------------

    The first step toward cloud success. - Migrate from AX to D365 with expert guidance from Microsoft. I'm Ready


  • 4.  RE: development: try catch understanding

    TOP CONTRIBUTOR
    Posted 13 days ago
    The ttslevel is used for nested tts blocks. I tried to stay away from that when I was programming, so I'm not sure how it acts in that situation.
    I did run into what looks to be a very well written blog on the subject - and current to D365 programming:
    https://www.sherweb.com/blog/dynamics-365/exception-handling-dynamics-365-operation/


    ------------------------------
    Tony Zeigler
    Senior Consultant
    Strategic Solutions NW
    Beaverton OR
    ------------------------------

    The first step toward cloud success. - Migrate from AX to D365 with expert guidance from Microsoft. I'm Ready


If you've found this thread useful, dive deeper into User Group community content by role