Exceptions

An exception is any erroneous behavior occurring during unit or form execution.

Causes of Exceptions

Exceptions may occur due to an error in a developer's code, unavailability of OS resources or incorrect operation of platform objects. Fore language implements the following class to work with exceptions: Exception. The Exception object is created to present an exception when it occurs. An exception is passed to the stack until it is handled by the application or until code execution is completed. The Exception class has a number of properties that make it easier to analyze an exception. Most classes inherited from Exception do not implement additional properties and methods, so the most important information on an exception can be found in the hierarchy of exception inheritance, in the exception name and in the information contained in this exception.

The Raise statement enables a developer to throw an exception when required.

Handling Exceptions

The Fore language has the Try … Except … Finally … End Try structure to handle exceptions. The Try keyword is followed by the block, execution of which may throw an exception.

The Except block is executed when an exception is thrown. This block may contain a code block executed if an exception occurs. This block may also contain the On...Do blocks. These blocks are sets of operations 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.

NOTE. The exception class and not the interface describing this class properties can be specified as a variable type. If exception class is unknown, the Exception base class is specified. The list of exception classes available to be used is given in the System Classes of Exceptions subsection.

The code block for handling exceptions is followed by the Do keyword.

The Finally block enables the user to execute some sequence of operators on exiting the block regardless of the execution results.

Exception Handling when Executing .NET Methods

Due to technology-related differences in languages, Fore does not enable the user to determine the class of exception thrown at the runtime of the .NET method and process the exception using an available system class. Exceptions of .NET methods can be handled only using the Exception base class.

The example of handling an exception that can be thrown when executing a custom method in a Fore.NET assembly with the UserAssm identifier:

Sub UserProc;
Var
    MB: IMetabase;
    Run: IForeNETRuntime;
    Asm: IForeNETRuntimeAssembly;
    Typ: IForeNETRuntimeType;
    TypInst: IForeNETRuntimeObjectInstance;
    v: Variant;
Begin
    MB := MetabaseClass.Active;
    Run := ForeNETAssemblyClass.Runtime;
    Asm := Run.Assembly(MB.ItemById("UserAssm").Bind As IForeNETAssembly);
    Typ := Asm.Type("UserAssm.UserClass");
    TypInst := Typ.CreateInstance;
    Try
        v := TypInst.InvokeMethod("Test", <Method parameters>);
    Except On e: Exception Do
        Debug.WriteLine(e.Message);
    Finally
    End Try;
End Sub UserProc;

See also:

System Classes of Exceptions | Operator Try … Except … Finally … End Try | Raise