Modifier

Partager via


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 errors 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 thrown by the Business Central AL platform. For on-premises, they can catch exceptions 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. For Business Central online, there are no restrictions on performing write transactions in try methods. For Business Central on-premises, the Business Central Server prevents database write transactions within try methods by default. If a try method contains a database write transaction, a runtime error occurs. You can allow write transactions by setting the DisableWriteInsideTryFunctions parameter in the Business Central Server configuration to false. Learn more about configuring the server in Configure Business Central Server. This behavior might change in an upcoming release.

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 true if no error occurs; false if an error occurs.

Note

The return value isn't accessible within the try method itself.

Getting details about errors

You can use the GetLastErrorText method to obtain errors generated by Business Central. To get details of exceptions generated by the AL platform or by .NET 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, and then set the TryFunction Attribute.

Example

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.

Failure modeling and robust coding practices
AL error handling
AL Simple Statements