Exception Handling In X++ with Dynamics 365 for Operations

By - November 10, 2016

playball1Not everything goes as planned in life or in programming. Programmers begin writing code to perform some automated task and make life easier for those using the program. They lay out a process in code that is expected to run thoroughly as designed, but there are times when things do not go as planned. Outside forces can impact the flow and cause exceptions or a divergence to the code flow or design. This is when programmers should use exception handling in their programming.

Programmers writing code or making modifications to Microsoft Dynamics 365 for Operations or Microsoft Dynamics AX who are using X++ should make use of Exception Handling.  But how exactly does one do that? What are the steps for proper exception handling?  It’s a lot like playing ball.

The first step in using exception error handling is for the program to “try.” When there is an area of code that could have unforeseen results, the programmer should begin the exception handling process by issuing a “TRY” statement. Within the TRY statement, the programmer will have code that they want to examine and prepare responses should an exception occur. This is where they will perform a “throw.”

Programmers can use the throw keyword to throw an Exception enum value. For example, the following statement throws an enum value as an exception:

throw Exception::error;

Instead of throwing an enum value, it is a best practice to use the Global::error method output as the operand for THROW:

throw Global::error(“The parameter value is invalid.”);

The Global::error method can automatically convert a label into the corresponding text. This helps programmers write code that can be more easily localized.

throw Global::error(“@SYS98765”);

After a THROW has been issued, the programmer will plan for the “catch.” When an exception is thrown, it is first processed through the catch list of the innermost try block.

If a catch is found that handles the kind of exception that is being thrown, program control jumps to that CATCH block. If the catch list has no block that specifies the particular exception, the system passes the exception to the catch list of the next innermost try block.

The CATCH statements are processed in the same sequence that they appear in the X++ code. It is common to have the first catch statement handle the Exception::Error enumeration value.



info(“In the ‘try’ block. (j3)”);

netString.Substring(-2); // Causes CLR Exception.


catch (Exception::Error)


info(“Caught ‘Exception::Error’.”);


catch (Exception::CLRError)


info(“Caught ‘Exception::CLRError’.”);

netExcepn = CLRInterop::getLastException();



There is an old saying, “If at first you don’t succeed, try, try again.” In baseball, you may not get it the first time, but you just try again. In the case of Exception Handling, the programmer can follow this motto and use the RETRY command in exception handling. For example, if the programmer is writing to a table that is already in use, the RETRY can cause the TRY to run again over and over, in case that table becomes available. Using RETRY at the end of a CATCH will cause the TRY to occur again and perhaps the code will go through without a hitch or a different CATCH may occur.

So, always remember: Working with Exception Handling can be just like playing ball; you TRY, THROW, CATCH, and RETRY.

Want more tips and techniques for getting the most out of Microsoft Dynamics 365 for Operations? Contact our experts at RSM or call 855-437-7201

by Sonia Burnette for RSM



Leave a Reply

Your email address will not be published. Required fields are marked *

Receive Posts by Email

Subscribe to the Microsoft Dynamics categories and receive notifications of new posts by email.
  • This field is for validation purposes and should be left unchanged.