Exception is any error behavior noticed on application code execution.
Exceptions may be caused by errors in developer code, unavailability of operating system resources, error work of platform objects or by other reasons. 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;
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 that handles an exception. This block mat also contain the On...Do blocks, which are a set of statements used for handling specific exceptions. The block starts with the On keyword followed by the handled exception class. One can specify the variable before the class, which will be used to access the exception information. The variable and the class are separated with a colon. The Do keyword is followed by the code block for handling exceptions.
NOTE. The exception class and not the interface describing this class properties/methods is specified as a variable type. If exception class is unknown, the Exception basic class is specified. The list of exception classes available to be used is given in the System Classes of Exceptions subsection.
The example of creating the Try…Except…Finally…End Try handlers:
Sub Main;
Begin
Try
// Code, calling of which may cause an error
Except
// Handle all error types
End Try;
Try
// Code, calling of which may cause an error
Except On Exception Do
// Handle all error types
End Try;
Try
// Code, calling of which may cause an error
Except
On ArgumentOutOfRangeException Do
//...
On e: IndexOutOfRangeException Do
//...
End Try;
End Sub Main;
The Finally block enables the user to execute some sequence of statements on exiting the block regardless of the execution results.
Sub Main;
Var
f: IFileStream;
Begin
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 the exception is handled that occurs when the non-existing file is opened. When handling the exception, the development environment console displays the error text, the unit or form identifier and the string number, execution of which caused the error. After this the code specified in the Finally block is executed.
See also: