Exceptions

Exception is any error behavior noticed on application code execution.

Causes of Exceptions

Exceptions may occur due to an error in a developer's code, unavailability of operating system resources or incorrect operation of platform objects. In the Fore language, exceptions are worked with using the Exception class. An object of the Exception class is created to present exception when it is thrown. 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 the developer to throw an exception when required.

Sub Main;
Begin
    
Try
        
Raise New Exception.Create("Exception");
    
Except On E: Exception Do
        Debug.WriteLine(
"Block Except. Message text: ");
        Debug.WriteLine(e.Message);
    
End Try;
End Sub Main;

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 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, specify the Exception basic class. 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 statements on exiting the block regardless of the execution results.

Sub Main;
Var
    a: Array[0..1Of Integer;
    f: IFileStream;
Begin
    Try
        a[2] := 1;
    Except On e: IndexOutOfRangeException Do
        Debug.WriteLine(e.Line);
        Debug.WriteLine(e.Source);
        Debug.WriteLine(e.Message);
    End Try;
    Try
        f := File.Open("c:\fileabcxyz666.txt", FileOpenMode.Read, FileShare.Exclusive);
    Except On e: FileNotFoundException Do
        Debug.WriteLine(e.Line);
        Debug.WriteLine(e.Source);
        Debug.WriteLine(e.Message);
    Finally
        Debug.WriteLine("This block is always executed");
    End Try;
End Sub Main;

After executing the example two exceptions are handled that are thrown when an element with an invalid index or a missing file is being called. When an exception is handled, the development environment console displays the error text, the unit or form identifier and the string number on execution of which the error occurs.

See also:

Fore Language Guide | System Classes of Exceptions