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

Exception::CLRError

(0) ShareShare
ReportReport
Posted on by 1,907
Hi All,

it seems dividing by zero, gets caught in the catch(Exception::CLRError).
So when to use catch(Exception::Error) in this case?
 
public static void main(Args _args)
{
    //System.Exception ex;
    try
    {
        int a = 1 / 0;
    }
    catch (Exception::CLRError)
    {
        error("error");
    }
    //catch (ex)
    //{
    //    error("error");
    //}
    catch (Exception::Error)
    {
        error("error");
    }
}


and what's the difference between  catch (Exception::CLRError) and catch (ex)??
Categories:
I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    236,570 Most Valuable Professional on at
    Exception::CLRError
    catch(Exception::Error) catches X++ errors, typically generated by throw error().
     
    catch (Exception::CLRError) is an old way of catching CLR exception, catch (ex) is a newer and better way - it directly gives you the exception, so you don't need to deal with things like getLastException(), extracting the actual exception from the reflection exception etc. Therefore consider catch (Exception::CLRError) as something legacy that shouldn't be used in new code.
     
    Nevertheless note that X++ exceptions are now CLR exceptions too - instances of Microsoft.Dynamics.Ax.Xpp.ErrorException class. If you want to distinct X++ errors and other CLR exceptions, use catch(Exception::Error) before catch (ex) (the order is important). If you don't need that, use catch() if you aren't interested in the exception object, and catch (ex) otherwise.

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 783

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 715 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 519 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans