X++ in AX7: Finally and using


Finally, X++ got support for the finally statement. It has exactly the same semantics as in C#. This means that you can now write: 

try 
{
}     
catch 
{
}      
finally 
{
}      
 

The contents of the finally block is guaranteed to be executed – regardless of exceptions or transactions. It is typically used to clean up any usage of none-managed resources. And to make that construct even cleaner, you can use the using keyword for types implementing the System.IDisposable interface.

using(var myObject = new MyObject()) 
{
    myObject.someMethod();  
}  
 

This is short hand for:

var myObject = new MyObject();  
try 
{
    myObject.someMethod();  

finally  
{
    myObject.Dispose();  
}  

One more thing…

Just like in C# the using statement can also be used to avoid providing fully qualified names when referencing .NET types. This means I can implement MyObject like this:

using System;  
class MyObject implements IDisposable   
{              
    public void Dispose()
    {
    }
}     

 

THIS POST APPLIES TO MICROSOFT DYNAMICS AX7 PREVIEW; IS PROVIDED AS-IS AND CONFERS NO RIGHTS.

Comments (5)

  1. NavidAhsan says:

    Hi Michael,

    The following example is taken from MSDN (msdn.microsoft.com/…/aa893385.aspx) and modified to add finally. Will the out put contain Cleanup_1? or just Cleanup_2?

    static void TryCatchTransaction5Job(Args _args)

    {

    /***

     Shows an exception that is thrown inside a ttsBegin – ttsCommit

    transaction block cannot be caught inside that block.

    ***/

       ;

       try

       {

           try

           {

               ttsbegin;

               try

               {

                   throw error("Throwing exception inside transaction.");

               }

               catch (Exception::Error)

               {

                   info("Catch_1: Unexpected, caught in 'catch' inside the transaction block.");

               }

               finally

               {

                  info("Cleanup_1");

               }

               ttscommit;

           }

           catch (Exception::Error)

           {

               info("Catch_2: Expected, caught in the innermost 'catch' that is outside of the transaction block.");

           }

           finally

           {

               info("Cleanup_2");

           }

       }

       catch (Exception::Error)

       {

           info("Catch_3: Unexpected, caught in 'catch' far outside the transaction block.");

       }

       finally

       {

          info("Cleanup_3");

       }

       info("End of job.");

    }

    1. msmfp says:

      As expected you’ll get all 3 messages from the finally blocks

  2. msmfp says:

    Hi Mike Frank – That is right, there is still no support for namespaces for X++ types.   I agree it would be great, and is high on my wish list too.

  3. Mike Frank says:

    The using statement for providing full qualified names would be more useful still, if X++ classes could be grouped into namespaces.

    Is that possible in AX 7?

    (I suppose not, because you state "when referencing .NET types" 🙁

  4. BPall says:

    Nice! 'using' includes the implementation of try/catch/finally block automatically? 🙂

Skip to main content