{"title":"System.GetLastErrorObject() Method","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/methods-auto/system/system-getlasterrorobject-method/","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/methods-auto/system/system-getlasterrorobject-method, 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/methods-auto/system/system-getlasterrorobject-method","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":"Gets the last System.Exception object that occurred.","date":"2026-10-01","meta":{"topic":"reference"},"outline":[{"level":2,"title":"Syntax","slug":"syntax","at":185,"chars":135},{"level":2,"title":"Return Value","slug":"return-value","at":320,"chars":56},{"level":2,"title":"Remarks","slug":"remarks","at":376,"chars":277},{"level":2,"title":"Example","slug":"example","at":653,"chars":975},{"level":2,"title":"Related information","slug":"related-information","at":1628,"chars":134}],"text":"> Version: _Available or changed with runtime version 2.0._\nGets the last System.Exception object that occurred.\n> NOTE\n> This method is supported only in Business Central on-premises.\n## Syntax\n```al\nDotNet :=   System.GetLastErrorObject()\n```\n> NOTE\n> This method can be invoked without specifying the data type name.\n## Return Value\nDotNet\nType: ../dotnet/dotnet-data-type\n## Remarks\nYou use this method to retrieve and handle the last exception that occurred in the application. The System.Exception object exposes several members that enable you to get detailed information about the exception, such Exception.InnerException and Exception.Message.\n## Example\nThis example uses the GetLastErrorObject method to get an exception object that occurs. In this example, the Microsoft .NET Framework objects are executed by MyCodeunit. The AL code uses the InnerException property of the System.Exception object to identify whether the inner exception has the type WebException and returns an exception message accordingly.\n```al\nvar\n    MyCodeunit: Codeunit MyCodeunit;\n    Exception: DotNet \"'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.WebException\";\n    WebException: DotNet \"'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Exception\";\nbegin\n    if not MyCodeunit.Run then begin\n        Exception := GetLastErrorObject;\n\n        if not Exception.InnerException.GetType.Equals(WebException.GetType) then\n            Error(Exception.Message);\n\n        WebException := Exception.InnerException;\n        Error(WebException.Message);\n    end;\nend;\n```\n## Related information\nsystem-data-type\nGet Started with AL<../../devenv-get-started>\nDeveloping Extensions<../../devenv-dev-overview>"}