Showing posts with label try catch finally and retry. Show all posts
Showing posts with label try catch finally and retry. Show all posts

Tuesday, July 13, 2021

Exception handling (Try / Catch ) blocks use in Dynamics 365 finance and operations

Use the throw,  try... catch,  finally,   and retry  to generate  and handle exceptions.

In transaction developments(ttsbegin/ttscommit),use the  exception  presented below:  

·        Deadlock -  Handling for deadlocks in the  database. The deadlock occurs  when multiple transactions are waiting for each other.

·        UpdateConflict -  Handling for UpdateSConflicts. The update conflict occurs  when a transaction is using OCC (Optimistic Concurrence Control). The transaction can be repeated.

·        DuplicateKeyException -  Handling for duplicate key field conflicts. Duplicity occurs when a transaction is using Optimistic Concurrence Control (OCC). The transaction can be repeated.


    #OCCRetryCount

    try

    {

ttsbegin;

        Your code here

        ttscommit;

    }

    catch (Exception: :D eadlock)

    {

        Retry on deadlock

        retry;

    }

    catch (Exception::UpdateConflict)

    {

        Retry to resolve conflict update

if (appl.ttsLevel() ==  0)

        {

            if (xSession::currentRetryCount() >= #RetryNum)

            {

                throw Exception::UpdateConflictNotRecovered;

            }

            else

            {

                retry;

            }

        }

        else

        {

            throw Exception::UpdateConflict;

        }

    }

    catch(Exception: :D uplicateKeyException)

    {

        Retry in case of a duplicate key conflict

if (appl.ttsLevel() ==  0)

        {

            if (xSession::currentRetryCount() >= #RetryNum)

            {

                throw Exception::D uplicateKeyExceptionNotRecovered;

            }

            else

            {

                retry;

            }

        }

        else

        {

            throw Exception::D uplicateKeyException;

        }

    }

Hope you find this useful, I will come up with another interesting blog post.