Handling Errors by Using Try Functions

Try functions in C/AL enable you to handle errors that occur in the application during code execution. For example, with try functions, you can provide more user-friendly error messages to the end user than those thrown by the system.

Behavior and Usage

The main purpose of try functions is to catch errors/exceptions that are thrown by Dynamics NAV or exceptions that are thrown during .NET Framework interoperability operations. Try functions catch errors similar to a conditional Codeunit.Run function call, except try function calls do not require that write transactions are committed to the database, and changes to the database that are made with a try function are not rolled back.

Database write transactions in try functions

Because changes made to the database by a try function are not rolled back, you should not include database write transactions within a try function. By default, the Microsoft Dynamics NAV Server configuration prevents you from doing this. If a try function contains a database write transaction, a runtime error occurs.


This behavior is different from the behavior in Microsoft Dynamics NAV 2016, which did not include this restriction by default. Therefore, you might encounter errors if you have application code that was written for Microsoft Dynamics NAV 2016 and you run the code in Microsoft Dynamics NAV 2017.

In practice, this means that you should not include the following function calls inside a try function scope:

Data Type Function
Record and RecordRef - INSERT
Database - COMMIT

If you want to change this behavior, you can set the DisableWriteInsideTryFunctions setting in the CustomSettings.config file of the server instance to false. However, we recommend that you design your code to suit the default behavior instead.

Handling errors with a return value

A function that is designated as a try function has a Boolean return value (true or false), and has the construction OK:= MyTryFunction. A try function cannot have a user-defined return value.

  • If a try function call does not use the return value, the try function operates like an ordinary function and errors are exposed as usual.

  • If a try function call uses the return value in an OK:= statement or a conditional statement such as IF-THEN, errors are caught.


The return value is not accessible within the try function itself.

Getting details about errors

You can use the GETLASTERRORTEXT Function to obtain errors that are generated by Dynamics NAV. To get details of exceptions that are generated by .NET Framework objects, you can use the GETLASTERROROBJECT Function to inspect the Expection.InnerException property.


The CRONUS International Ltd. demonstration database includes codeunit 1291 DotNet Exception Handler that includes several global functions for handling exceptions similar to a try-catch capability in C#. You can use this codeunit together with try functions to handle exceptions and maximize the reuse of code.

Creating a Try Function

To create a try function, add a function in C/AL code of an object (such as a codeunit) as usual, and then set the TryFunction Property property to Yes. A try function has the following restrictions:

In test and upgrade codeunits, you can only use a try function on a normal function type, as defined by the FunctionType Property (Test Codeunits) or FunctionType Property (Upgrade Codeunits).


The following example illustrates the use of a try function together with codeunit 1291 DotNet Exception Handler to handle .NET Framework Interoperability exceptions. The code is in text file format and has been simplified for illustration. The CallTryPostingDotNet function runs the try function TryPostSomething in a conditional statement to catch .NET Framework Interoperability exceptions. Errors other than IndexOutOfRangeException type are re-thrown.

PROCEDURE TryPostingSomething@1();  

PROCEDURE CallTryPostingDotNet @2();  
  MyPostingCodeunit@1 : Codeunit 90;  
  MyDotNetExceptionHandler@2 : Codeunit 1291;  
  IndexOutOfRangeException@3 : DotNet 'mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IndexOutOfRangeException'  
  IF TryPostingSomething THEN  
    MESSAGE('Posting succeeded.')  
    IF MyDotNetExceptionHandler.TryCastToType(IndexOutOfRangeException) THEN  
      MESSAGE('The index used to find the value was not valid.')  

See Also

C/AL Function Statements