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 thrown by the system.

Note

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.Run method call, except try method calls do not require that write transactions are committed to the database, and changes to the database that are made with a try method are not rolled back.

Database write transactions in try methods

Because changes made to the database by a try method are not rolled back, you should not 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 cannot have a user-defined return value.

  • If a try method call does not 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 true if no error occurs; false if an error occurs.

Note

The return value is not 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 Exception.InnerException property.

Creating a try method

To create a try method, add a method in the AL code of an object such as a codeunit as usual, and then set the TryFunction Attribute.

Example 1

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

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.

See Also

AL Simple Statements