Exceptions

Exceptions provide a structured, uniform, and type-safe way of handling both system level and application level extraordinary (error) conditions.

Exceptions can be thrown due to the following reasons:

Handling Exceptions

Use the Try…Except…Finally…End Try statement to handle exceptions. Specify the block, which execution may throw an exception, after the Try keyword.

The Except block is executed if an exception is thrown. This block may contain a code block executed if an exception is thrown. This block may also contain the On...Do blocks. These blocks are sets of operators starting from the On keyword, which is followed by the type of the handled exception: variable name, colon and variable type - handled exception class. The specified variable will be used to access information about the exception. The System.Exception class or any other class that inherits from System.Exception is specified as a variable type.

Try
    
//...
    //Execute code, in which error may occur
    //...
Except On Ex: System.Exception Do
    
Begin
        
//Handle exception
    End;
On Ex: System.ArgumentOutOfRangeException Do
    
Begin
        
//Handle exception
    End;
Else
    
Begin
        
//Handle all the other exceptions
    End;
Finally
    
//Block that is always executed
End Try;

Exceptions on Executing Fore Methods

The Fore language is based on using COM technology. If an exception is thrown when using system properties or methods of the Fore language, use the System.Runtime.InteropServices.COMException class to handle such exceptions.

If an exception is thrown when using properties or methods implemented in custom Fore assemblies, use the System.Exception class to handle such exceptions.

Example

The example of handling an exception, which can be thrown when executing the system Fore method IMetabaseDefinition.Open:

Public Shared Sub Main(Params: StartParams);
Var
    MB: IMetabase;
    Sp: ISecurityPackage = 
New StandardSecurityPackage();
Begin
    
Try
        Mb := Params.Metabase;
        Mb.Definition.Open(Sp.CreateCredentials(AuthenticationMode.amPassword),
            ApplicationMode.amWin, LocaleCodeID.lcidRussian);
    
Except On Ex: System.Runtime.InteropServices.COMException Do
        System.Diagnostics.Debug.WriteLine(Ex.Message);
    
Finally
    
End Try;
End Sub;

The example of handling an exception, which may be thrown on executing the custom method implemented in the Fore assembly with the UserAssm identifier:

Public Shared Sub Main(Params: StartParams);
Var
    Svc: IForeServices;
    Run: IForeRuntime;
    Assembly: IForeAssembly;
    FClass: IForeClass;
    FSub: IForeSub;
    FObj: IForeObject;
Begin
    Svc := Params.Metabase 
As IForeServices;
    Run := Svc.GetRuntime();
    Run.LoadAssembly(
"UserAssm");
    Assembly := Run.BindToAssembly(
"UserAssm");
    FClass := Assembly.BindToClass(
"UserClass");
    FObj := FClass.CreateObject();
    FSub := FClass.BindToMethod(
"Test");
    FSub.Self := FObj;
    
Try
        FSub.Invoke();
    
Except On Ex: System.Exception Do
        System.Diagnostics.Debug.WriteLine(Ex.Message);
    
Finally
    
End Try;
End Sub;

See also:

Fore.NET Language Guide