Thursday 20 December 2018

Try Catch Exception handling AX 2012/ D365 Finance & Operations X++

The code below contains a try/catch that we use a lot when developing batch jobs, especially multi-threaded ones.

It deals with frequently occurring exceptions that, in some cases, can be easily solved by retrying/continuing:


-> Deadlocks
-> Update conflicts
-> Duplicate key conflicts


#OCCRetryCount
 
    try
    {
        ttsbegin;

        // do stuff here
     
        ttsCommit;
    }
    catch (Exception::Deadlock)
    {
        // retry on deadlock
        retry;
    }
    catch (Exception::UpdateConflict)
    {
        // try to resolve update conflict
        if (appl.ttsLevel() == 0)
        {
            if (xSession::currentRetryCount() >= #RetryNum)
            {
                throw Exception::UpdateConflictNotRecovered;
            }
            else
            {
                retry;
            }
        }
        else
        {
            throw Exception::UpdateConflict;
        }
    }
    catch(Exception::DuplicateKeyException)
    {
        // retry in case of an duplicate key conflict
        if (appl.ttsLevel() == 0)
        {
            if (xSession::currentRetryCount() >= #RetryNum)
            {
                throw Exception::DuplicateKeyExceptionNotRecovered;
            }
            else
            {
                retry;
            }
        }
        else
        {
            throw Exception::DuplicateKeyException;
        }
    }


Please note that retry keyword is retrying the same record again and again. Continue keys word is skip the present record and continue with the next record.

Example: 

while (gQueryRun.next())
        {
            inventJournalTable    = gQueryRun.get(tableNum(inventJournalTable));

            if (inventJournalTable.Posted == NoYes::No)
            {
                JournalCheckPost                journalCheckPost;
                journalCheckPost = InventJournalCheckPost::newPostJournal(inventJournalTable);
                try
                {
                    ttsbegin;
                    if(journalCheckPost.validate())
                    {
                        journalCheckPost.run();
                    }
                    ttscommit;
                }
             
                catch (Exception::Deadlock)
                {
                    continue;
                }
                catch (Exception::Error)
                {
                    continue;
                }
                catch (Exception::Warning)
                {
                    CLRInterop::getLastException();
                    continue;
                }
                catch (Exception::CLRError)
                {
                    CLRInterop::getLastException();
                    continue;
                }
            }
        }

15 comments:

  1. Thanks for picking out the time to discuss this, I feel great about it and love studying more on this topic. It is extremely helpful for me. Thanks for such a valuable help again. comment devenir rentier

    ReplyDelete
  2. Finances exist on the state level and also on the manufactures and branches' level too, and in such conditions, when the most part of the manufactures are not state. loans

    ReplyDelete
  3. I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work. view website

    ReplyDelete
  4. Thanks for the blog filled with so many information. Stopping by your blog helped me to get what I was looking for. Now my task has become as easy as ABC. source

    ReplyDelete
  5. This latest first appears to be a part of value of main industrial funds, later it is moved to the cost price of a ready product (that is to the value too) and after its realization, and it is set the depression fund. Dollar General

    ReplyDelete
  6. It's important that you know what rates and fees will be applied to your merchant services account. selling credit card machines

    ReplyDelete
  7. However, because a merchant is dealing with sensitive financial information, it is important to have security protocols in place to prevent fraud. Merchant Account Referral Program

    ReplyDelete
  8. In order How to be a Payment Processing Company in 2022, you must have a strong understanding of the latest payment processing technologies and be able to offer your clients a variety of payment processing options. You must also be able to provide your clients with a high level of customer service and support.

    ReplyDelete
  9. I learn some new stuff from it too, thanks for sharing your information. How to be a Payment Processor

    ReplyDelete
  10. Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also Selling Merchant Services

    ReplyDelete
  11. I have seen the best content ever. I will suggest choosing these Best Loan Company in Jaipur

    Personal Loan Agency in jaipur
    HDFC Personal Loan in Jaipur

    ReplyDelete
  12. Very efficiently written information. It will be beneficial to anybody who utilizes it, including me. Keep up the good work. For sure i will check out more posts. This site seems to get a good amount of visitors. https://catcherrors.com/

    ReplyDelete
  13. I felt astoundingly happy while examining this site. This was really outstandingly illuminating site for me. I really favored it. This was genuinely a true post. Thankful!. Testogen

    ReplyDelete

Adding a newline into a string in C# and X++

Below is the sample code we can use for  adding a newline after every occurrence of "@" symbol in the string in C#   using System...