System.GetLastErrorObject() Method

Version: Available or changed with runtime version 2.0.

Gets the last System.Exception object that occurred.

Note

This method is supported only in Business Central on-premises.

Syntax

DotNet :=   System.GetLastErrorObject()

Note

This method can be invoked without specifying the data type name.

Return Value

DotNet
 Type: DotNet

Remarks

You 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.

Example

This 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.

var
    MyCodeunit: Codeunit MyCodeunit;
    Exception: DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.WebException";
    WebException: DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Exception";
begin
    if not MyCodeunit.Run then begin  
        Exception := GetLastErrorObject;  
      
        if not Exception.InnerException.GetType.Equals(WebException.GetType) then  
            Error(Exception.Message);  
      
        WebException := Exception.InnerException;  
        Error(WebException.Message);  
    end;  
end;

System Data Type
Get Started with AL
Developing Extensions