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 :
Microsoft Dynamics AX (Archived)

Try Catch - Throw Error in class method

(1) ShareShare
ReportReport
Posted on by 107

Hi guys,

I have the following issue:

Code example:

ClassMyClass myClass;

try
{

myClass = ClassMyClass::construct();

myClass.doSomething();

}
catch
{

*** Handle error ***

}

Now the question:

Inside the myClass.doSomething(), a validation does a throw error(XXX);

I would expect that my catch is hit and i can handle the error, but instead the error gets trown and i'm not hitting my catch() statement.

Is this normal? What can i do to make sure my catch is getting hit?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    David Massey Profile Picture
    on at

    Take a look at this documentation.  Pay attention to how try/catch works within a transaction scope (ttsBegin, ttsCommit, ttsAbort).

    msdn.microsoft.com/.../aa893385.aspx

  • BuBbA Profile Picture
    107 on at

    There is no transaction so it shouldn't matter. To test, i tried it with and without a transaction scope but it made no difference. The code just stops at the throw error, no code is executed after, where i would suspect my catch to be hit.

  • Hariharans87 Profile Picture
    3 on at

    Catch should work if you throw the error message

    e.g.

    throw error('Error message');

    But, if you use error method without throw, it won't work like

    e.g.

    error('Error message');

  • BuBbA Profile Picture
    107 on at

    I thought it should work, but it doesn't. The class i call is a standard Ax class (SpecTransManager) and it does use "throw error(xxx)" but my catch statement is never hit, code just stops on the throw error.

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

    Please put a breakpoint on doSomething() and check the transaction level in debugger. If it's not zero, it's the transaction to blame,

  • BuBbA Profile Picture
    107 on at

    Yep, that was the tip! When i hit my breakpoint, TTS level was 1, where i expected it to be 0. Apparently in a class my class inherits from, a colleague added a transaction.

    Thank you!

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans