Well well well! They finally added this! In a way

As all of you are aware one beautiful thing that a lot of programming languages have is the “Try-Catch” statement. C/AL didn’t have this though. And some people never thought it would.

If you are curious about this and you want to go into more detail I invite you to check Vjeko’s blog entry for more information.

As it happens, Microsoft thought this would be a neat thing to have, and they added a property when defining the functions as follows:


Yey! Finally! Let me copy-paste a small text from the help regarding this property:

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

So what it basically says is that it is very similar to what we were able to do using codeunits.

One of the biggest advantages to me is not having to use one new codeunit everytime we need to “trycatch” a process. This can end up consuming a lot of codeunits in some cases.


I tried a very simple and quick example just to see it working. To do so I created a new codeunit and added a new function named “MyTryFunction” and set the “TryFunction” property to true. Here you go!



And if you guessed that it would end up giving me a message saying “NOT OK”, you have guessed right :-)



I’m going to start thinking how I’m going to redesign some of my developments with this :-)



The post How To: Try-Catch in C/AL for NAV2016 appeared first on Code4NAV.com.