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

Announcements

No record found.

News and Announcements icon
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,166 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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 617

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 461 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 298 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans