web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

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

Rahul Kiran Profile Picture Rahul Kiran 481
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;
                }
            }
        }

This was originally posted here.

Comments

*This post is locked for comments