raise exception Instruction

The raise exception instruction raises an exception.

Syntax

The syntax of the raise exception instruction is:

raise expression [internal | precondition];

The expression value is a reference to an instance of the Exception class (or one of its subclasses). The optional internal or precondition keyword specifies the type of exception, which is precondition by default.

Description

Specify an optional internal or precondition keyword in the raise exception instruction if you want to specify if the exception is an internal error or a precondition exception on the raise exception instruction.

As the raise exception instruction is implemented only for user exceptions, do not use it to raise object locked exceptions.

A precondition exception reports precondition contract exception where the caller has failed to satisfy precondition requirements of the invoked method. When a method raises a precondition exception, it signifies that its caller has failed to meet certain requirements such as passing valid parameters or ensuring a required state in objects used by the invoked method for the called method to do its job. When a precondition exception is raised at run time, the exception object contains two method descriptor references: currentMethodDesc describing the calling method and reportingMethodDesc describing the reporting method (that is, the method that raised the exception).

When an internal exception is raised, currentMethodDesc describes the method raising the exception and reportingMethodDesc contains a null reference. The default exception handler and dialog display information from the current method descriptor and the reporting method descriptor, if present.

Example

The following example shows a method used to raise an exception of type MyException.

raiseMyException(code: Integer; category: Integer);
vars
    exObj : MyException;
begin
    create exObj;
    exObj.errorCode := code;
    exObj.category  := category;
    raise exObj;
end;

For more information about raising exceptions, see Chapter 3, "Exceptions".