Handling Errors using Try Methods
Try methods in AL enable you to handle errors that occur in the application during code execution. For example, with try methods, you can provide more user-friendly error messages to the end user than those that are thrown by the system.
Try Methods are available from runtime version 2.0.
Behavior and usage
The main purpose of try methods is to catch errors/exceptions that are thrown by Business Central or exceptions that are thrown during .NET Framework interoperability operations. Try methods catch errors similar to a conditional Codeunit. Except for the try method, the Run method call doesn't require that write transactions are committed to the database, and changes to the database that are made with a try method aren't rolled back.
Database write transactions in try methods
Because changes made to the database by a try method aren't rolled back, you shouldn't include database write transactions within a try method. By default, the Business Central Server configuration prevents you from doing this. If a try method contains a database write transaction, a runtime error occurs.
Handling errors with a return value
A method that is designated as a try method has a Boolean return value (true or false), and has the construction
OK:= MyTrymethod. A try method can't have a user-defined return value.
If a try method call doesn't use the return value, the try method operates like an ordinary method, and errors are exposed as usual.
If a try method call uses the return value in an
OK:=statement or a conditional statement such as
if-then, errors are caught. The try method returns
trueif no error occurs;
falseif an error occurs.
The return value isn't accessible within the try method itself.
Getting details about errors
You can use the GetLastErrorText method to obtain errors that are generated by Business Central. To get details of exceptions that are generated by .NET Framework objects, you can use the GetLastErrorObject method to inspect the
Creating a try method
To create a try method, add a method in the AL code of an object such as a codeunit, and then set the TryFunction Attribute.
The following simple example illustrates how the try method works. First, create a codeunit that has a local method
MyTrymethod. Add the following code on the
OnRun trigger and
trigger OnRun() begin MyTrymethod; message('Everything went well'); end;
local procedure MyTryMethod() begin error('An error occurred during the operation'); end;
When you run this codeunit, the execution of the
OnRun trigger stops. The error message
An error occurred during the operation is thrown in the UI.
Now, set the TryFunction Attribute of the
MyTrymethod method. Then, add code to the
OnRun trigger to handle the return value of the try method:
[TryFunction] local procedure MyTryMethod() begin error('An error occurred during the operation'); end; trigger OnRun() begin if MyTryMethod then message('Everything went well') else message('Something went wrong') end;
When you run the codeunit, instead of stopping the execution of the
OnRun trigger when the error occurs, the error is caught and the message
Something went wrong is returned.