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

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Get the last error string

(0) ShareShare
ReportReport
Posted on by

How can I know the last error string by using try + catch blocks ?

Thanks :)

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Denis Patrakov Profile Picture
    on at
    Re: Get the last error string

    You can store the number of lines in the infolog before entering a try block:

    int startLine = infolog.line();

    And in the catch block get all the messages that have been added in the try block:

    str msg;
    int currentLine;
    // ...
    for (currentLine = startLine + 1; currentLine <= infolog.line(); currentLine++)
    {
        msg += infolog.text(currentLine);
    }

    To get only the last message you can use a code like this:

    str msg;
    // ...
    if (infolog.line() > startLine)
    {
        msg = infolog.text();
    }
  • Community Member Profile Picture
    on at
    Re: Get the last error string

    I think you didn't understand my question.

    I don't know what happens in the try block (It's a 3rd party function - I cannot change - kind of "black-box") but I know that this function throws an exception.

    I want to know what kind of exception (what message, what message-no, if any, etc ...) How can I know that ?

    Thanks :)

  • Denis Patrakov Profile Picture
    on at
    Re: Get the last error string

    If we are still talking about AX then there are just a few situations when you cannot change the code you're calling from X++ and all of them are related to some kind of interop: COM/.NET/DLL. Is this your case?

  • Community Member Profile Picture
    on at
    Re: Get the last error string

    We are still talking about AX.

    I don't understand : Can I get the last error string or not? If I can - then how

    (I just need to know what to tell the 3rd party company what error in order to get in progress).

    Thanks :)

  • Verified answer
    Denis Patrakov Profile Picture
    on at
    Re: Get the last error string

    Yes, you can get but... There can be four main scenarios in your case:

    • You call X++ code - in that case you can use the sample code provided earlier;
    • If you call .NET code - use AifUtil::getClrErrorMessage();
    • If you call a DLL function - use WinAPI::getLastError() or   WinAPIServer::getLastError() and then format the message according to error code;
    • If you invoke a COM object - use COM.getError() to get a COMError object an retrieve the message via COMError.description().

    If you directly call .NET/COM/DLL and try to get the error message from the infolog then you'll get a "stub" message like "An error occurred when invoking CLR class". But if you process an order then you most likely call X++ code so you can just call infolog.text().

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Community Member Profile Picture

Community Member 4

#2
Guy Terry Profile Picture

Guy Terry 2 Moderator

#2
Nayyar Siddiqi Profile Picture

Nayyar Siddiqi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans