Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Extending Microsoft Dynamics NAV 2016 Cookbook

You're reading from   Extending Microsoft Dynamics NAV 2016 Cookbook Extend Dynamics NAV 2016 to win the business world

Arrow left icon
Product type Paperback
Published in Jan 2017
Publisher Packt
ISBN-13 9781786460608
Length 458 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Alexander Drogin Alexander Drogin
Author Profile Icon Alexander Drogin
Alexander Drogin
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Writing Basic C/AL Code FREE CHAPTER 2. Advanced C/AL Development 3. Reporting and Data Analysis 4. .NET Interoperability in C/AL 5. Extending C/AL with COM Components 6. SharePoint Integration 7. Control Add-ins 8. Web Services 9. Events and Extension Packages 10. PowerShell

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 codeunit, the OnRun trigger will call the function:

            GetDataFromRemoteSource; 
    

    Then save and run the codeunit. The function fails, as expected:

  3. This exception interrupts code execution and might frighten the user with the technical details...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image