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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)
Answered

Get thrown .NET object on error

(0) ShareShare
ReportReport
Posted on by 175

Hi,

When catching a CLR error, is it possible to get the .NET object that is thrown?

For example "System.Net.HttpWebRequest" throws "System.Net.WebException" when the HTTP status not 200.

How can I get the System.Net.WebException object in X++?

In C# it would be as simple as this: https://stackoverflow.com/questions/3614034/system-net-webexception-http-status-code.

By that syntax does not exist in X++.

Any ideas?

Luke

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    239,747 Most Valuable Professional on at

    Yes, it is. The first step is calling CLRInterop::getLastException() to get the exception. But one more step is needed. Because .NET Interop from X++ uses reflection to call .NET code, the original exception is wrapped in a TargetInvocationException. You need to get its inner exception.

    try
    {
        ...
    }
    catch (Exception::CLRError)
    {
        invocationException = CLRInterop::getLastException();
        invocationException.get_InnerException(); // this will give you the original exception
    }
  • Community Member Profile Picture
    on at

    Hi Martin,

    It won't help, I tested in AX 2012 environment, there's no exception thrown. The class exited at the line I expected an exception

    your method works with other .net class but not this System.Net.WebException.

    I tried to replicate the class in C# and received System.Net.WebException: 'The remote server returned an error: (404) Not Found.'

    but there's nothing in AX

  • Martin Dráb Profile Picture
    239,747 Most Valuable Professional on at

    I don't believe that. I don't see any reason why WebException would behave differently.

    Make sure that you're testing it in the right context. I guess that you code runs inside a transaction, therefore you catch statement is completely ignored.

  • Community Member Profile Picture
    on at

    hi Martin,

    there's a similar case

    community.dynamics.com/.../753199

    hope that you believe now!

  • Martin Dráb Profile Picture
    239,747 Most Valuable Professional on at

    The verified answer of the other tread says that it indeed was caused by a transaction. So you either have your answer, or your case isn't the same.

  • Community Member Profile Picture
    on at

    hi Martin,

    there's no transaction (no ttsbegin) in my code.

    I finally found that the error was caused by calling webrequest in an environment that requires webproxy.

  • Martin Dráb Profile Picture
    239,747 Most Valuable Professional on at

    I wasn't talking about ttsBegin in your code. Most likely your complete code was executed inside a transaction started at a higher level.

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 16

#2
Nagendra Varma K Profile Picture

Nagendra Varma K 4

#2
Harisgillani Profile Picture

Harisgillani 4

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans