{"title":"AL error handling","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/devenv-al-error-handling/","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/devenv-al-error-handling, 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/devenv-al-error-handling","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":"Deal with unexpected situations that occur when code is run in AL for Business Central.","date":"2024-03-01","meta":{"topic":"concept-article"},"outline":[{"level":2,"title":"Error handling methods in AL","slug":"error-handling-methods-in-al","at":1124,"chars":1340},{"level":2,"title":"Error handling strategies","slug":"error-handling-strategies","at":2464,"chars":1541},{"level":2,"title":"Guidelines for error messages","slug":"guidelines-for-error-messages","at":4005,"chars":464},{"level":2,"title":"Error telemetry (emitted by the AL runtime)","slug":"error-telemetry-emitted-by-the-al-runtime","at":4469,"chars":977},{"level":2,"title":"Error telemetry (emit your own)","slug":"error-telemetry-emit-your-own","at":5446,"chars":326},{"level":2,"title":"Related information","slug":"related-information","at":5772,"chars":701}],"text":"> NOTE\n> We're working to improve the onboarding experience for AL developers. If you have feedback about this article, use the Feedback section at the bottom of the article to tell us what you'd like to see.\n> We also welcome contributions to our documentation. Learn more about contributing in Contribute to the help<../help/contributor-guide#contributing>.\nThe AL language has many error handling features, which can help you deal with unexpected situations that occur when code is run. This section contains articles about using these methods in AL to handle errors that occur during code execution, while making sure that your application has a consistent user interface.\nThe following articles discuss the different aspects of error handling in AL:\n Failure modeling and robust coding practices<devenv-robust-coding-practices>\n Understanding the error dialog<devenv-error-dialog>\n User experience guidelines for errors<devenv-error-handling-guidelines>\n devenv-actionable-errors\n Collecting errors<devenv-error-collection>\n devenv-handling-errors-using-try-methods\n Understanding the error dialog<devenv-error-dialog>\n## Error handling methods in AL\nFor examples of using different error handling methods in AL, see the following table:\nMethod|Description\nmethods-auto/system/system-clearlasterror-method|Removes the last error message from memory.\nmethods-auto/dialog/dialog-data-type|Represents a dialog window.\nmethods-auto/dialog/dialog-error-errorinfo-method|Displays an error message and ends the execution of AL code.\nmethods-auto/errorinfo/errorinfo-data-type|Contains a set of methods that helps identify errors, classify these errors, send errors to telemetry and display UI messages. Go to the data type article for an overview.\nmethods-auto/system/system-data-type|Is a complex data type and contains multiple methods for getting and classifying errors.\nmethods-auto/system/system-getlasterrorcallstack-method|Gets the call stack from where the last error occurred.\nmethods-auto/system/system-getlasterrorcode-method|Gets the classification of the last error that occurred.\nmethods-auto/system/system-getlasterrorobject-method|Gets the last System.Exception object that occurred.\nmethods-auto/system/system-getlasterrortext--method|Gets the last error that occurred in the debugger.\n> TIP\n> If you already know the name of a data type, use the Filter by title field in the upper left corner, above the table of contents to find the topic faster.\n## Error handling strategies\nFor examples of different error handling strategies, see the following table:\nIf you want to...|Then use\nRun a codeunit and decide to do something if an error occurs.|`if not Codeunit.run()`. See Codeunit.Run return value<methods-auto/codeunit/codeunit-run-method>\nCheck for an error and show an error dialog to the user.|`Dialog.Error(Message: ErrorInfo)`. See methods-auto/dialog/dialog-error-errorinfo-method.\nCheck for an error and show an error dialog to the user with added support information.|Using the ErrorInfo Data Type with the Error Method<methods-auto/errorinfo/errorinfo-data-type>\nDo bulk validations in AL and not show an error dialog for each of them to the user.|Collecting Errors<devenv-error-collection>\nCatch errors raised by other AL methods|devenv-handling-errors-using-try-methods\nCatch errors/exceptions that are thrown during .NET framework interoperability operations (on-premises only).|Handling .NET Exceptions using Try Methods<devenv-handling-errors-using-try-methods>\nHandle errors happening in Page Background Tasks|The OnPageBackgroundTaskError trigger of the page. See devenv-page-background-tasks#handling-errors.\nLog an error that happens within a database transaction (that rollback)|Either log the error in a new session using a background session, or log the error to telemetry (see methods-auto/session/session-logmessage-string-string-verbosity-dataclassification-telemetryscope-dictionary[text,text]-method or ../administration/telemetry-feature-telemetry#LogError ).\n## Guidelines for error messages\nImproving error handling and error messages reduces friction for the users and highly impacts the user experience. Clear, informative error messages help users understand what went wrong and how to correct it, reducing frustration, and improving user satisfaction.\nFor guidance on how to formulate good error messages, see Error message best practices - what to say?<devenv-error-handling-guidelines#error_message_best_practices>.\n## Error telemetry (emitted by the AL runtime)\nWhen a user gets an error dialog while working in Business Central, a telemetry signal is emitted, which can be logged in an Azure Application Insights resource. This telemetry data let's you identify and analyze where and why the error occurs. You can also set up alerts in Azure Application Insights to get notified if many users experience errors.\nThere are three types of telemetry data emitted about errors\n Error message quality\n Error dialogs shown to users\n Permission errors\nIf you want to...|Then look at this telemetry\nUnderstand if users understand the error messages they see.|Error message quality telemetry<../administration/telemetry-error-message-voting-trace>\nUnderstand which error dialogs users see and where in the AL code the error is raised.|../administration/telemetry-error-method-trace\nUnderstand when users get error dialogs about lack of permissions.|../administration/telemetry-permission-error-trace\n## Error telemetry (emit your own)\nYou can emit your own telemetry about error situations by using the Telemetry AL module. Using this module, you can simplify the way you monitor the health of your app and also monitor the uptake and usage of application features.\nSee ../administration/telemetry-feature-telemetry#LogError.\n## Related information\nFailure modeling and robust coding practices<devenv-robust-coding-practices>\nUnderstanding the error dialog<devenv-error-dialog>\nUser experience guidelines for errors<devenv-error-handling-guidelines>\ndevenv-actionable-errors\nCollecting errors<devenv-error-collection>\ndevenv-handling-errors-using-try-methods\ndevenv-page-background-tasks#handling-errors\n../administration/telemetry-feature-telemetry#LogError\nError message quality telemetry<../administration/telemetry-error-message-voting-trace>\n../administration/telemetry-error-method-trace\n../administration/telemetry-permission-error-trace\ndevenv-al-control-statements\nAL development environment<devenv-reference-overview>"}