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. You can use try functions to catch errors/exceptions that are thrown by Microsoft 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 with the following differences:

  • Try function calls do not require that write transactions are committed to the database.

  • Changes to the database that are made with a try function are not rolled back.

Important

Before you use try functions in an application, read more about their usage at NAV Design Pattern: TryFunction – .NET Exception Handling in C/AL.

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 to Yes. A try function has the following restrictions:

Understanding Try Function Behavior and Usage

A function that is designated as a try function has a Boolean return value (true or false). A try function has the construction OK:= MyTryFunction.

  • 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.

Note

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

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

Tip

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.

Example

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.

[TryFunction]
PROCEDURE TryPostingSomething@1();
BEGIN
  CODEUNIT.RUN(CODEUNIT::"Purch.-Post");
END;
PROCEDURE CallTryPostingDotNet @2();
VAR
  MyPostingCodeunit@1 : Codeunit 90;
  MyDotNetExceptionHandler@2 : Codeunit 1291;
  IndexOutOfRangeException@3 : DotNet 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IndexOutOfRangeException'
BEGIN
  IF TryPostingSomething THEN
    MESSAGE('Posting succeeded.')
  ELSE BEGIN
    MyDotNetExceptionHandler.Collect;
    IF MyDotNetExceptionHandler.TryCastToType(IndexOutOfRangeException) THEN
      MESSAGE('The index used to find the value was not valid.')
    ELSE
      MyDotNetExceptionHandler.Rethrow;
  END;
END;

See Also

Other Resources

C/AL Function Statements