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 :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Exception::CLRError

(0) ShareShare
ReportReport
Posted on by 1,552

CLRError, could be web Exception or Exception (maybe there might be sth else)

catch(Exception::CLRError)
{
    System.Exception ex = CLRInterop::getLastException().GetBaseException();
    error( ex.get_Message());

}

so here if i use System.Exception and it was web exception i will get a null reference...so how do i handle this?

I have the same question (0)
  • Suggested answer
    Mea_ Profile Picture
    60,286 on at

    Try to check that it is not null first, like shown here https://docs.microsoft.com/en-us/dynamicsax-2012/developer/how-to-catch-exceptions-thrown-from-clr-objects#ee677495collapse_allen-usax60gifx-code-sample and get details in the same way.

  • junior AX Profile Picture
    1,552 on at

    If i remove .getBaseException() as the link says, it now checks if it's null or not instead of getting an error directly

    System.Exception ex = CLRInterop::getLastException();
    if (ex != null)
    {
    }

    But if i do the opposite use web exception first without getBaseException(), i get an error before checking if it's null

    System.Net.WebException webEx = CLRInterop::getLastException();
    if (webEx!=null)
    {
    }

    So what to do in this case?
    and what other types of error does CLR catch?

  • Suggested answer
    Martin Dráb Profile Picture
    240,534 Most Valuable Professional on at

    Don't use Exception::CLRError and CLRInterop::getLastException() anymore; it can be done in an easier way (in D365FO, not in older versions):

    System.Net.WebException webEx;
    
    try
    {
        ...
    }
    catch (webEx)
    {
    	error(webEx.Message);
    }

  • junior AX Profile Picture
    1,552 on at

    so i should define all possible scenarios

    System.Net.WebException webEx;
    System.Exception Ex;
    try
    {
    }
    
    catch(webEx)
    {
    }
    catch(Ex)
    {
    }

    What about (Exception::Error)? i used to put catch for clr,and one for error and a generic one

    catch(Exception::CLRError)
    {
    
    } 
    catch (Exception::Error)
    {
                    
    }
    
    catch
    {
                    
    }

  • Martin Dráb Profile Picture
    240,534 Most Valuable Professional on at

    You should use those you need - no more, no less. I can't tell you whether you need to handle X++ errors in a special way and therefore you need "catch (Exception::Error)" block. It clearly depends on what you're doing. If you don't need it, remove the block. And definitely don't use empty blocks for exceptions that you don't want to handle, because the result would be the opposite (the exception would get handled, there just wouldn't be any extra logic at the time of handling).

  • junior AX Profile Picture
    1,552 on at

    I do have logic inside catch(webEx) and catch(Ex) and the generic catch

    My question is since u said there is no need to use Exception::clr and use catch (Ex) and catch(WebEx) instead  then is there also a way to not use Exception::error? Is there sth else?

  • Martin Dráb Profile Picture
    240,534 Most Valuable Professional on at

    You asked about CLR exceptions, so that's what we discussed. But normal, non-CLR exceptions still exist and they can be handled in the usual way. Therefore you can use Exception::Error if you want to catch native X++ exceptions.

  • junior AX Profile Picture
    1,552 on at

    Ok one last thing

    Since Catch(Ex) can catch both Ex and webEx so instead of doing two catches (where the first catch should be webEx then Ex) can't i from one catch which is Ex inside find they type if it is web or normal exception and do logic accordingly?

  • Martin Dráb Profile Picture
    240,534 Most Valuable Professional on at

    Yes, you cam do that, but normally it's easier to simply catch the right type straight away.

  • junior AX Profile Picture
    1,552 on at

    If my code in they try is related to web response then is there a need to catch(Ex). I mean if u know the code in the try would Ex occur out of no where? Or a case that i don't know about.. Or in such cases i should put the Exceptions i'm sure of and anything else the general catch should catch

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 Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 341 Most Valuable Professional

#2
CU10121822-0 Profile Picture

CU10121822-0 312

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 262 Super User 2026 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans