TryFunction Attribute

Version: Available or changed with runtime version 1.0.

Specifies the method to be a try method, which can be used to catch and handle errors and exceptions that occur when code is run.

Applies To

  • Method

Syntax

Version: Available or changed with runtime version 2.0.

[TryFunction]
procedure TryFunction()

Remarks

Note

In test and upgrade codeunits, this property only applies to normal methods as specified by the Normal Attribute (Test Codeunits) or MethodType Property (Upgrade Codeunits).

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. You can use try methods to catch errors/exceptions that are thrown by Business Central or exceptions that are thrown during .NET Framework interoperability operations.

For more information, see Handling Errors by Using Try Methods.

Example

The IsSecureHttpUrl method throws an error when trying to make a web request using an insecure URL. By setting the TryFunction attribute on it, the exception can be caught and handled by the OnRun trigger.

[TryFunction]
procedure IsSecureHttpUrl(Url: Text)
var
    Uri: DotNet Uri;
begin
    IsValidUri(Url);
    Uri := Uri.Uri(Url);
    if Uri.Scheme <> 'https' then
        Error(NonSecureUriErr);
end;
trigger OnRun()
var
    URL: Text;
begin
    if not IsSecureHttpUrl(URL) then begin
        message('URL is not secure.')
        exit(true);
    exit(false);
end;

See Also

AL Method Reference
Essential AL Methods
Method Attributes
Handling Errors by Using Try Methods
Properties