Mastering Microsoft Dynamics NAV 2016
上QQ阅读APP看书,第一时间看更新

Exception handling

Exception handling is a new concept in Microsoft Dynamics NAV. It was imported from .NET, and is now gaining popularity among the C/AL programmers because of its effective usage.

Like C#, for exception handling, we use the Try function. The Try functions are the new additions to the function library, which enable you to handle errors that occur in the application during runtime. Here we are not dealing with compile time issues. For example, the message Error Returned: Divisible by Zero Error. is always a critical error, and should be handled in order to be avoided. This also stops the system from entering into the unsafe state. Like C sharp and other rich programming languages, the Try functions in C/AL provide easy-to-understand error messages, which can also be dynamic and directly generated by the system. This feature helps us preplan those errors and present better descriptive errors to the users.

You can use the Try functions to catch errors/exceptions that are thrown by Microsoft Dynamics NAV or exceptions that are thrown during the .NET Framework interoperability operations.

The Try function is in many ways similar to the conditional Codeunit.Run function except for the following points:

  • The database records that are changed because of the Try function cannot be rolled back
  • The Try function calls do not need to be committed to the database

Creating a Try function

Let's see how the Try function can be created in C/AL. For the sake of example, let's create a codeunit: 50001 - Exception Handle.

Phase 1
  1. Create a global function called Try.
  2. Define two parameters for the Try function:
            ItemCode of Type code and Length 20 
            ItemDescription of Type Text and Length 50 
    
  3. In the property section of the Try function, change the value of the TryFunction property to YES. This will help this function to be identified as the Exception handler in the system, which can be called from another codeunits:

    Phase 1

  4. Let us write code to push a new item record into the item table. So, create a new record type variable called Itemrec.
  5. Write the following simple code to push a value into the item record:
            [TryFunction] Try(ItemCode : Code[20];ItemDescription :   
              Text[50]) 
     
            Itemrec.INIT; 
            Itemrec."No." := ItemCode; 
            Itemrec.Description:= ItemDescription; 
            Itemrec.INSERT; 
    
  6. Save the codeunit, and follow phase 2.
Phase 2

In phase 2, we will create a new codeunit, 50002 - Application, which will show how the codeunit created in phase 1 can be utilized. Actually, we will call the function through this application codeunit where the exception will he handled:

  1. Create a function called AddItem with no parameter.
  2. Create a variable of codeunit 50001- Exception Handle as ExceptionHandle:

    Phase 2

  3. Write the code to provide the exception message if the process meets any exception. If the item is already there in the current case, then print the error message with the description presented by the inbuilt functions called GETLASTERRORCODE and GETLASTERRORTEXT. And in case of success, present a success message:
            LOCAL AddItem() 
     
            CLEARLASTERROR; 
            IF ExceptionHandle.Try('12345','MyTestItem') THEN 
              MESSAGE('Item added Successfully') 
            ELSE 
              MESSAGE('Error Returned Error : %1  -  %2',  
                GETLASTERRORCODE,GETLASTERRORTEXT); 
    
  4. Also call this local function, AddItem, into the OnRun trigger as follows:
            OnRun() 
              AddItem 
    
  5. Finally, run the Codeunit 50002- Application.

    The first time you run it, a new item with item number 12345 will be created:

    Phase 2

  6. Run the codeunit once again; since the item is already present, it will generate an error message as seen in the following screenshot:

Phase 2

Hence, we achieved the exception handling functionality in Microsoft Dynamics NAV, which is very exciting to implement and use.