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 call another method in a 'try' block in case an exception is thrown inside a transaction

(0) ShareShare
ReportReport
Posted on by 678

Hi All,

I would like to call a method in a 'try' block in case an exception is thrown inside a transaction.

my code can be simplified as follows:  

while (enumerator.moveNext())
{
	try
	{
		ttsbegin;
		...
		purchLine.update()
		
		ttscommit;
	}
	catch
	{
        infoLogEnum = SysInfoLogEnumerator::newData(infolog.infologData());
        while(infoLogEnum.moveNext())
        {
            // currentException() returns you Exception enum and you can check if it is Exception::Error or Exception::Warning.
            switch (infoLogEnum.currentException())
            {
                case Exception::Warning:
                    info(strFmt("[Error] PO:%1 %2",purchLine.PurchId,infoLogEnum.currentMessage()));
                    break;
            }
        }
	}
}

I would like to include that call to another method within the tts begin- tts commit transaction block like below

while (enumerator.moveNext())
{
	try
	{
		ttsbegin;
		...
		purchLine.update()
		// here I would like to call another method before ttscommit 
		
		ttscommit;
	}
	catch
	{
        infoLogEnum = SysInfoLogEnumerator::newData(infolog.infologData());
        while(infoLogEnum.moveNext())
        {
            // currentException() returns you Exception enum and you can check if it is Exception::Error or Exception::Warning.
            switch (infoLogEnum.currentException())
            {
                case Exception::Warning:
                    info(strFmt("[Error] PO:%1 %2",purchLine.PurchId,infoLogEnum.currentMessage()));
                    break;
            }
        }
	}
}

I tried to add that call right after purchline.update(), but then the infolog message is not shown to the user for a line with an error  in case multiple lines are selected for update.

Thank you. 

I have the same question (0)
  • AJJAIS Profile Picture
    on at

    Hi Shawn,

    Can you please explain more what are you trying to achieve. If there is an exception then call will go to exception and there you can set more messages as per your requirement. Thank you.

  • Verified answer
    Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    If exception is thrown, not other code in the transaction will be executed. Neither your code not ttscommit (the transaction is aborted, not committed). Therefore your requirement doesn't make sense.

    You can call your code either in 'catch' blocks or the 'finally' block, but not inside 'try'.

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

    As others have already told you, it's not possible. When an exception is thrown, the execution always jumps to the catch statement.  However I'm sure there's a solution to your business requirement, whatever it might be.

    Meanwhile it's good to study the exception handling of x++ since it's an elementary part of development: docs.microsoft.com/.../exception-handling-with-try-and-catch-keywords

    Whenever you write solutions that create or update data, you must take into account the possible failures, how to handle them, how to inform the user, and what things must fail together.

  • ShawnDEV Profile Picture
    678 on at

    Hi,

    I will make  my question a bit clearer. "Where should I put a call to another method if I want to  call that method only if the result of purchLine.update() is not caught in error in the above situation where I need a separate transaction for each line ?"

    Thank you.

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

    Hi,

    what do you mean "result of purchLine.update() is not caught in error"?

    If you want to run code if purchLine.update() doesn't throw exception, put your code below purchLine.update().

    If you want to run code if purchLine.update() throws exception, put that code in the catch block.

  • ShawnDEV Profile Picture
    678 on at

    Hi,

    Yes, that's why I have tried to put the code below purchLine.update() which is still within a transaction.

    Then, when testing with one line with an error which is caught in catch block where the infolog is shown to the user, it shows the infolog with an error. it works fine.

    But, when testing with two lines where one line with an error and the other without an error at one go, the infolog error message for that line with an error is not shown to the user at all. I debugged it and the message is stacked in SysInfologEnumerator > data object well.

    I have no clue what excludes this message when selecting more than 1 record.

    Thank you.

  • nmaenpaa Profile Picture
    101,162 Moderator on at

    Ok, so is your question still about calling code, or showing something in infolog?

    In other words: is your problem that

    - some code is not called (like your original question indicates)

    - your infolog enumerating logic in catch block doesn't work as you would expect

    - something else?

  • Suggested answer
    ergun sahin Profile Picture
    8,826 Moderator on at
  • ShawnDEV Profile Picture
    678 on at

    Hi,

    It is similar to the following problem - showing in infolog:

    - your infolog enumerating logic in catch block doesn't work as you would expect

    Are there any ways of showing infolog in catch statement correctly?

    I also see that duplicate messages are shown in the infolog each time I try updating.

    pastedimage1617964876483v1.png

    Thank you. 

  • nmaenpaa Profile Picture
    101,162 Moderator on at

    What if you remove your infolog logic from the catch statement? Does it then show the messages only one time?

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
Martin Dráb Profile Picture

Martin Dráb 451 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans