{"title":"TryFunction attribute","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/attributes/devenv-tryfunction-attribute/","by":"JOP-AL","license":"Copyright (c) Microsoft Corporation. CC BY 4.0, https://creativecommons.org/licenses/by/4.0/; code samples MIT. From https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/attributes/devenv-tryfunction-attribute, MicrosoftDocs/dynamics365smb-devitpro-pb at 828b6ee (2026-11-19). Changed: includes filled in, links to this site, and for agents shortened without dropping a fact.","credit":"Microsoft Corporation, CC BY 4.0","origin":"https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/attributes/devenv-tryfunction-attribute","source":{"name":"Business Central developer and IT pro docs","repo":"https://github.com/MicrosoftDocs/dynamics365smb-devitpro-pb","commit":"828b6ee72fcd75755627cd6011f3d20899217ab2","date":"2026-11-19"},"checked":"2026-11-19","description":"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.","date":"2026-10-01","meta":{"topic":"reference"},"outline":[{"level":2,"title":"Applies to","slug":"applies-to","at":190,"chars":22},{"level":2,"title":"Syntax","slug":"syntax","at":212,"chars":118},{"level":2,"title":"Remarks","slug":"remarks","at":330,"chars":675},{"level":2,"title":"Example","slug":"example","at":1005,"chars":634},{"level":2,"title":"Related information","slug":"related-information","at":1639,"chars":199}],"text":"> Version: _Available or changed with runtime version 1.0._\nSpecifies the method to be a try method, which can be used to catch and handle errors and exceptions that occur when code is run.\n## Applies to\n Method\n## Syntax\n> Version: _Available or changed with runtime version 2.0._\n```al\n[TryFunction]\nprocedure TryFunction()\n```\n## Remarks\n> NOTE\n> In test and upgrade codeunits, this property only applies to normal methods as specified by the Normal Attribute \\(Test Codeunits\\)<devenv-normal-attribute> or ../devenv-methodtype-property-upgrade-codeunits.\nTry 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.\nSee ../devenv-handling-errors-using-try-methods.\n## Example\nThe IsSecureHttpUrl method raises an error when a URL doesn't start with `https://`. The TryFunction attribute lets the OnRun trigger detect and handle the error without stopping execution.\n```al\ncodeunit 50100 TryFunctionExample\n{\n    trigger OnRun()\n    begin\n        if not IsSecureHttpUrl('http://www.contoso.com') then\n            Message('The URL is not secure.');\n    end;\n\n    [TryFunction]\n    local procedure IsSecureHttpUrl(Url: Text)\n    begin\n        if CopyStr(Url, 1, 8) <> 'https://' then\n            Error(NonSecureUrlErr);\n    end;\n\n    var\n        NonSecureUrlErr: Label 'The URL must use HTTPS.';\n}\n```\n## Related information\nAL Method Reference<../methods-auto/library>\n../devenv-essential-al-methods\ndevenv-method-attributes\n../devenv-handling-errors-using-try-methods\n../properties/devenv-properties"}