Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

Catching X++ Errors in .NET

Posted on by

Is there any way to get the error message that is thrown from x++ code when it is caught in C# code?  There are a lot of articles about how to handle a CLR exception from X++ code.  However, I am unable to find anything to explain how I can handle an X++ error that is thrown to .NET code. 

I am working in AX 2012.  I have added a .NET project under the AOT > Visual Studio Projects > C Sharp Projects.  In this C# project, my .NET code calls into an X++ class.  The X++ method throws an error.  When the error is thrown, I can see in the visual studio debugger that it is of type Microsoft.Dynamics.Ax.Xpp.ErrorException.  The message in this exception is "Exception of type 'Microsoft.Dynamics.Ax.Xpp.ErrorException' was thrown." 

Back in the .NET code, the exception is caught as a System.Reflection.TargetInvocationException where the inner exception is the Microsoft.Dynamics.AX.Xpp.ErrorException.  Is there no way to get the error message that was thrown from x++ code when it is caught in the C# code?

Any help would be greatly appreciated! 

*This post is locked for comments

  • Greg's Mom Profile Picture
    Greg's Mom on at
    RE: Catching X++ Errors in .NET

    Martin,

    Thanks a lot for your help and the detailed information on exception handling between X++ and .NET.  I hope this is something we see an improvement on in future versions.  

    Another note regarding dealing with exceptions and .NET interop, we found while trying to work around this issue.  When .NET code calls into an X++ class, if an error is thrown from the X++ class, it is caught by the first catch in the .NET code.  We tried creating a wrapper class in X++ that would call into the other X++ class that throws the error but the catch statement in the X++ wrapper class was skipped and the exception was caught by .NET.  I'm not sure if this is known behavior but it appears to behave like the entire call to X++ proxy from .NET is wrapped in a tts transaction.  

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: Catching X++ Errors in .NET

    You can see my findings here: Exception handling with X++ and .NET Interop. Unfortunately it probably won't help much you with your problem, but knowing a bit more can't harm.

  • Greg's Mom Profile Picture
    Greg's Mom on at
    RE: Catching X++ Errors in .NET

    I had thought to work around this by creating a string error message property in my x++ class, handle the exception in the x++ code and store the error message in the class instance which my .net code could access once the call to x++ code has completed.  

    Unfortunately, this didn't work as the catch in my x++ code was ignored.  When the x++ code throws the error, the x++ catch is skipped and the exception is caught by the .NET catch statement as a System.Reflection.TargetInvocationException.  

    I'm wondering if the call to the proxy is inside a transaction so the catch inside the x++ code does not get hit?

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: Catching X++ Errors in .NET

    Okay, that will be the difference - if you invoke your code from a batch, there is nothing running in X++ at all, therefore AX doesn't use the interop layer.

    It seems to me that there is an error handler on AOS (and AOS only, not on client) that swallows exception details - give me some time, I'll try to investigate that.

  • Greg's Mom Profile Picture
    Greg's Mom on at
    RE: Catching X++ Errors in .NET

    Also, I forgot to mention that yes, I am using proxy classes by adding the class from visual studio application explorer to my project.

  • Greg's Mom Profile Picture
    Greg's Mom on at
    RE: Catching X++ Errors in .NET

    Thanks for the suggestion, Martin.  This is a good point.  

    I had started by trying to catch the Microsoft.Dynamics.AX.ManagedInterop.ErrorException but the catch would never hit and instead it would fall down to the generic catch System.Exception.  That's when I noticed the type on the exception was System.Reflection.TargetInvocationException and the inner exception was the Microsoft.Dynamics.Ax.Xpp.ErrorException.  

    I should note that this is a batch job running on the server so that may account for the different type of exception being thrown?  

    Thanks again for your input!

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: Catching X++ Errors in .NET

    How do you invoke the X++ code? If I use an X++ proxy, the exception object returned is Microsoft.Dynamics.AX.ManagedInterop.ErrorException, not Microsoft.Dynamics.Ax.Xpp.ErrorException. That's also the expected behavior as per MSDN documentation. Microsoft.Dynamics.AX.ManagedInterop.ErrorException contains the infolog message in the Message as usual, so there there shouldn't be any problem when using proxies.

    Update: Actually, I see a difference between client- and server-bound runs. I'll do a bit more research.

  • Greg's Mom Profile Picture
    Greg's Mom on at
    RE: Catching X++ Errors in .NET

    Thanks for the quick reply, Joris.  This was my fear.  With the move to more interoperability between .NET and X++, it would be nice if we could get an improvement here.  There's always ways to rewrite this to handle the exception on the X++ side and then return a string that includes the error message to the .NET code but this is not really the preferred way of dealing with exceptions.

  • Verified answer
    Joris dG Profile Picture
    Joris dG 17,775 on at
    RE: Catching X++ Errors in .NET

    I'll have to try this to give you an exact answer, but I'm pretty sure the message itself is lost, as in AX those are not associated with the exception, they are just put in the infolog and then an exception is thrown sort of independently. I would expect you'd be able to get the exception enum at least (through the data property on the exception?), to find out a bit more detail about the exception. But that exception enum is really the only thing AX has as far as detailed exceptions, it's a bit of a pain.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans