Examples of Defining an Exception Handler

The following example shows the definition of an exception handler method that has only the mandatory parameter (ex, in this example) that refers to the NormalException object.

exceptionHandler(except: NormalException):Integer;
vars
    number : Integer;
begin
    // This is the exception handling method that is specified when arming
    // the handler.  It will be called when the appropriate exceptions are
    // raised.
    // If the error code of the exception is 1035, the exception is
    // identified as being the String Too Long exception and is handled
    // appropriately.
    if except.errorCode = 1035 then
        number := app.msgBox("String too long.  Resume execution after
                             method?", "String too long", 52);
        if number = MsgBox_Return_Yes then
            // The Ex_Resume_Next return value will pass control back to the
            // method that armed the exception handler (in this case,
            // method1) and resume execution after the invocation of the
            // method that raised the exception.
            return Ex_Resume_Next;
        else
            // The Ex_Abort_Action return value will cause all currently
            // executing methods to be aborted.  In this case, the
            // application will revert to execution after the invocation of
            // the method that raised an idle state, and await further user
            // input.
            status.caption := "Aborting all currently executing methods";
            return Ex_Abort_Action;
        endif;
    // If the error code of the exception is 64000, the exception is
    // identified as being the user-defined exception that was assigned this
    // code, and is handled appropriately.
    elseif except.errorCode = 64000 then
        number := app.msgBox("User-defined exception.  Continue method
                             execution?", "User-defined exception", 52);
        if number = MsgBox_Return_Yes then
            // The Ex_Continue return value will pass control back to the
            // method that raised the exception handler (in this case,
            // method4) and resume execution after the raising of the
            // exception.
            return Ex_Continue;
        else
            // The Ex_Resume_Next return value will pass control back to the
            // method that armed the exception handler (in this case,
            // method1) and resume execution after the invocation of the
            // method that raised the exception.
            status.caption := "Resuming execution after exception throwing
                               method invocation";
            return Ex_Resume_Next;
        endif;
    endif;
end;

For examples of the methods used to arm the exception handler defined in the above method, see "Examples of Arming an Exception Handler", later in this chapter. For an example of the method that creates an object of the UserException class, defines the properties for this object, and then raises the exception, see "Using the raise exception Instruction", later in this chapter.

The following example shows the definition of an exception handler method that has only the mandatory parameter (exObj, in this example) that refers to the exception object.

handleException(exObj: Exception): Integer;
constants
    StringTooLong = 1035;
begin
    if exObj.errorCode = StringTooLong then
        app.msgBox("The picture is too large: ", "Error", MsgBox_OK_Only);
        return Ex_Abort_Action;
    else
        return Ex_Pass_Back;    // pass exception to prior handler
    endif;
end;