O'Reilly logo

Extending Microsoft Dynamics NAV 2016 Cookbook by Alexander Drogin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling exceptions with try functions

The TryFunction property has been introduced to C/AL to handle exceptions thrown by .NET objects. It allows NAV application developers to catch exceptions and handle them the way the try...catch statement would in C#.

How to do it...

The following example explains how to declare a try function and catch exceptions from .NET objects.

  1. To illustrate why we need the TryFunction attribute at all, let's create a codeunit with a function that will fail and throw a .NET exception. Let's call the GetDataFromRemoteSource function and suppose it is intended to load data from some web resource:
            HttpWebRequest := HttpWebRequest.Create('http://unknown'); 
            HttpWebResponse := HttpWebRequest.GetResponse; 
    
  2. In the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required