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)

Catching X++ Errors in .NET

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Verified answer
    Joris dG Profile Picture
    17,775 on at

    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.

  • Greg's Mom Profile Picture
    on at

    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.

  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    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
    on at

    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!

  • Greg's Mom Profile Picture
    on at

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

  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    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
    on at

    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?

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

    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
    on at

    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.  

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