When coding in AL, there are certain times when we need to avoid errors from stopping a specific process. So they have to be handled differently. In this post, you will learn 3 ways to handle errors in Microsoft Dynamics 365 Business Central.
- Try Functions
- Handle Codeunit.Run
- Collectible Errors
Try Functions
The way to define a try function is simple. We just need to add [TryFunction] before the procedure.
![](https://businesscentralgeek.com/wp-content/uploads/2022/12/image-56.png)
The code inside a try function won´t stop in errors. And they all return a boolean. It will return true if the code runs successfully and false if it encountered an error. So, in the next image, only the error in the red square will be executed.
![](https://businesscentralgeek.com/wp-content/uploads/2022/12/image-45.png)
The main thing to know is that changes to the database within a try function aren’t rolled back. So, you shouldn’t include database write transactions inside a try function. Make sure to check Stefano´s post talking about it:
However, we can include validations. In this example, we try to validate an invented payment method that does not exist in the database.
![](https://businesscentralgeek.com/wp-content/uploads/2022/12/image-46.png)
As you can see in this video, once the payment method is created, it can be modified.
Get more information about collectible errors here:
Handle Codeunit.Run
This method was the first error-handling mechanism for older versions of Business Central. The way to use it is similar to a try function.
By executing Codeunit.Run, it returns a boolean. Returning true, if it was successful, and false if it encountered an error.
You can find a simple example here:
![](https://businesscentralgeek.com/wp-content/uploads/2022/12/image-57.png)
In the base app, we can also find some examples. In this image, Codeunit.Run is used for releasing a purchase document. If it succeeded, it returns true. Otherwise, it returns false.
![](https://businesscentralgeek.com/wp-content/uploads/2022/12/image-47-1024x667.png)
Get more info about this way of handling errors here:
Collectible Errors
Collectible errors were introduced in BC19 (2021 Wave 2). It´s a way to get multiple errors while the code is executing. In this scope, the process won´t stop at errors and will continue until it finishes.
First, we have to define the error behavior property before the procedure name.
![](https://businesscentralgeek.com/wp-content/uploads/2022/12/image-55.png)
Then, we need to define the ErrorInfo data type. And add the properties you can see in the image below.
In this example, emails are sent in a loop. If an email couldn´t be sent, an error is collected.
Finally, the code continues until the process is finished. Note that the line Error(ErrInfo) doesn´t stop the process. It´s only been collected for later use.
![](https://businesscentralgeek.com/wp-content/uploads/2022/12/image-52.png)
After the final message and once the process is finished, we can see all the errors in the message that it´s displayed. With ErrorInfo.Create we can define any error message:
![](https://businesscentralgeek.com/wp-content/uploads/2022/12/image-53.png)
If you want to learn 5 ways to send e-mails in Business Central follow the article below:
Get more information about collectible errors here:
For the full information about error handling strategies follow the Microsoft documentation below:
https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-al-error-handling
That´s all. Hope you find it helpful.