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 :
Finance | Project Operations, Human Resources, ...
Answered

How to use Try and catch for batch execution

(0) ShareShare
ReportReport
Posted on by

Hi all,

Need some advice. If I want to use Try and Catch, for execute a class, running a batch job class in code. how are we use the try and catch looks like ?

I mean what would be the Catch ? is the batch job succeeded or fail can be captured and determine that result (Fail or succeed) in the "Catch"? what would be the Exception should we use? and what if, when it is succeeded, if lets say this batch job class is call within a While.. select loop, and when succeeded, I want to update one field in the table that used by the While...select. is it still inside the Try ? or in Catch section ?

Thanks,

I have the same question (0)
  • Suggested answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi WWwong,

    What are the criteria for the "batch job succeeded"? If you use exception handling, you can process errors during job execution and exit execution as succeeded.

    For your particular case with "while select" you would need to update the record at the end of "try" section and you can use generic catch without exception type.

    Also, keep in mind nuances about try-catch statements inside transactions docs.microsoft.com/.../xpp-exceptions

    while select forupdate myTable
    {
        try
        {
            ttsBegin;
            
            //Do some stuff
            
            myTable.MyField = "Success";
            myTable.update();
    
            ttscommit;
        }
        catch
        {
            error("Oops");
        }
    }

  • Community Member Profile Picture
    on at

    Hi Sergei,

    Noted, so if inside the try {...} I'm calling/run a batch job class, it is inside ttsbegin or outside ?

    Like you example :

    while select forupdate myTable

    {

       try

       {

           ttsBegin;

           //Do some stuff

           myTable.MyField = "Success";

           myTable.update();

           ttscommit;

       }

       catch

       {

           error("Oops");

       }

    }

    if lets say, there is a code like :

    MyClass    cls = new MyClass();

    cls.run();

    this 2 line of code, is before ttsbegin, or inside ttsbegin and ttscommit ?

    and do we need to put ttsabort in that catch ?

    Thanks,

  • Verified answer
    nmaenpaa Profile Picture
    101,162 Moderator on at

    Please check this doc: docs.microsoft.com/.../exception-handling-with-try-and-catch-keywords

    Running something in batch doesn't make exception handling any different.

    You put the ttsebgin and ttscommit where you want to, so I don't understand your last question in your previous message. The batch framework itself doesn't add any transaction scope around your class.

  • Verified answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi WWwong,

    I would say that in most cases you need to put the processing part and final update in one transaction to keep data integrity. In case if an update of MyField for some reason fails MyClass logic will be reverted as well in this case.

    You don't need to use ttsAbort in catch section. If an error is thrown inside transaction ttsAbort executed automatically.

  • Community Member Profile Picture
    on at

    Alright.

    Thanks both.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 490 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 429 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 241 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans