Properly handle Errors in your Nav/365 solutions

When we’re focus in our code, we do not alway anticipate about error handling. This can be very frustrating when we encouter field triggers error while testing and remade some piece of code to handle it.

If you don’t know about, you can avoid this with two methods.

  1. TryFunction : You can set a function “TryFunction” property to yes. If error appear in a TryFunction and other code called under it, it can be catched in the parent code and simply return a TRUE or FALSE depending if an error has occured. This work same as a Codeunit.RUN function :
    IF NOT TryFunction() THEN BEGIN
      // Do some manual error handling
      MESSAGE(GETLASTERRORTEXT);
    END ELSE BEGIN
      // Continue your process
    END;
  2. ASSERTERROR : write this keyword before a function calling, this while catch all error occuring in it and avoid code execution to stop. This could be useful with standard not Tryfunction methods.
    CLEARLASTERROR;
    ASSERTERROR NotTryFunction();
    IF GETLASTERRORTEXT <> '' THEN BEGIN
      // Do some manual error handling
      MESSAGE(GETLASTERRORTEXT);
    END ELSE BEGIN
      // Continue your process
    END;

Ms doc on ASSERTERROR statement (their talking about test function but it’s also working with normal code)

https://docs.microsoft.com/en-us/dynamics-nav/c-al-asserterror-statements

Leave a Reply

Your e-mail address will not be published. Required fields are marked *