HP 3000 Manuals

Trapping Run-Time Errors [ HP FORTRAN 77/iX Reference ] MPE/iX 5.0 Documentation


HP FORTRAN 77/iX Reference

Trapping Run-Time Errors 

The MPE/iX implementation of FORTRAN 77 provides a trap handling
mechanism that allows you to control how a program interruption is
handled.  An interruption may be handled by:

   *   Executing a specified procedure.

   *   Ignoring the interruption.

   *   Aborting the program.

The trap-handling mechanism is initiated with the ON statement.

Whenever a major error occurs during the execution of a program, of a
hardware instruction, of a procedure from the System Library, or of a
user-called intrinsic, your program normally aborts and an error message
is printed.  You can change this action by establishing traps for any of
the following kinds of interruptions:

   *   Arithmetic errors.
   *   Basic external function errors.
   *   Internal function errors.
   *   Control-Y user interrupts.

This is a program that traps external errors:

            PROGRAM TEST
            REAL*8 A
            ON EXTERNAL ERROR CALL ERRORHANDLE
            A = 8.0
            PRINT *, "01   DACOS(A) ", DACOS(A)
            PRINT *, "Normal Exit from Main"
            END

            SUBROUTINE ERRORHANDLE(ERRORNUM, RESULT, OP1, OP2)
            INTEGER*4 ERRORNUM
            REAL*8 RESULT, OP1, OP2
            PRINT *, "Control returned to SUBROUTINE ERROR"
            PRINT *, "Internal Error occured ERROR NUMBER = ", ERRORNUM
            PRINT *, "What error number to be passed to caller = "
            READ *, ERRORNUM
            PRINT *, "What result to be passed to caller  = "
            READ *, RESULT
            PRINT *, "oop1 = ", OP1
            END

This program shows how an internal error can be trapped to the
user-defined error recovery routine ERRORHANDLE.

     *   ERRORNUM is the error that is generated in function DNUM.
     *   RESULT is the result computed before the error occurs.  The
     *          computation process is not yet complete.
     *   OPERAND1 is the operand that is passed to DNUM.
     *
     *   ERRORNUM can be changed in the trap routine ERRORHANDLE.  When the error
     *   number is set to zero, then a normal termination sequence occurs and
     *   the standard error message prints.  When the error number is set to
     *   a non-zero value, a user defined result can be passed back by the
     *   trap routine.  The error number that is modified in the trap routine
     *   won't modify the error generated in DNUM.
     *
            PROGRAM TESTING
            REAL *8 A
            ON INTERNAL ERROR CALL ERRORHANDLE
     100    A = DNUM('A')
            PRINT *,A
     *      GENERATE ERROR 61 ** Number out of range **
            A = DNUM('12.000E+8934')
            PRINT *,A
            GOTO 100
            END

            SUBROUTINE ERRORHANDLE(ERRORNUM, RESULT, OPERAND1, NUMBER)
            INTEGER*4 ERRORNUM
            REAL*8 RESULT
            CHARACTER OPERAND1*(*)
            INTEGER*2 NUMBER
            PRINT *, "Control returned to SUBROUTINE ERROR"
            PRINT *, "Internal Error occured ERROR NUMBER = ", ERRORNUM
            PRINT *, "What error number to be passed to caller = "
            READ *, ERRORNUM
            PRINT *, "What result to be passed to caller  = "
            READ *, RESULT
            PRINT *, "Operand1 = ", OPERAND1
            PRINT *, NUMBER
            END

This program shows how a divide by zero can be trapped for libf math
function FTN_DTOD(a,b).

     *  If ERRORNUM = 0, the program will ABORT.  A non-zero value for
     *  ERRORNUM causes the function to return the value of the result.
     *
     *      The result in the error recovery must match the parameter that was
     *      passed.
     *
     *      This will cause Internal error 68.
     *
            PROGRAM TESTING
            REAL *8 A, B, C
            INTEGER*4 I, J, K
            ON INTERNAL ERROR CALL ERRORHANDLE
            C = -2.0
            A = 0.0
            B = A**C
            PRINT *, "DtoD B= ", B
            PRINT *, "Main End."
            END

            SUBROUTINE ERRORHANDLE(ERRORNUM, RESULT)
            INTEGER*4 ERRORNUM
            REAL*8 RESULT
            PRINT *, "Control returned to SUBROUTINE ERROR"
            PRINT *, "Internal Error occured ERROR NUMBER = ", ERRORNUM
            PRINT *, "What error number to be passed to caller = "
            READ *, ERRORNUM
            PRINT *, "What result to be passed to caller  = "
            READ *, RESULT
            END

Refer to the HP Compiler Library/iX Reference Manual for more information
about trapping errors.

Trap Actions 

The action taken after an interrupt is trapped depends on the
specification in the most recently executed ON statement for that
interrupt condition.

   *   If ABORT was specified, a standard error message is generated and
       the program is aborted.

   *   If IGNORE was specified, processing continues with the next
       instruction.

       If the condition causing the interrupt is an integer division by
       zero, the result is set to zero.  For other conditions, the
       previous content of the target register is supplied as the result.
       IGNORE is particularly valuable for preventing Control-Y
       interrupts at inconvenient times in a program.

   *   If CALL was specified, the normal (ABORT) error message is
       suppressed, and control is transferred to the specified trap
       procedure.

       Zero or more arguments describing the error are passed to the trap
       procedure, which can attempt to analyze or recover from the error,
       or can execute some other programming path specified by the user,
       such as an alternate return.

       Further details are given in the following sections.

Arithmetic Trap Procedure.   

For the ON INTEGER*2 OVERFLOW statement to be effective, the
CHECK_OVERFLOW INTEGER compiler option must also be enabled.  This is the
default on MPE/iX.

If emulated floating point numbers have been selected by the HP3000_16
directive, any reference in the ON statement to the INEXACT or ILLEGAL
trap is ignored, the traps are not set, and the compiler generates a
warning message.

In each of the above cases, the corresponding trap requires one reference
parameter that is of the same type as that associated with the error
condition.  When the trap is called, the parameter is the result of the
operation that caused the trap to be invoked.

System Trap Procedure.   

The trap procedure that is called for system errors must have one
parameter that is an integer array.  The link editor performs parameter
type checking.  When the trap procedure is called, the contents of the
parameter is an array of eight parameters defined by the system.  For
more details, refer to the Trap Handling manual.  This parameter group
immediately follows the parameters to the intrinsic in which the error
occurred.

Basic External Function Trap Procedure.   

The trap procedure that is called for external function errors must have
four formal arguments in the following order:

   1.  A single integer containing the error number that is determined by
       the external function in which the error occurred.

   2.  The result.

   3.  The first operand.

   4.  The second operand.

If the trap procedure returns normally and has set the error number
(first argument) to zero, a standard message is printed and the program
aborts.

Internal Function Trap Procedure.   

The trap procedure that is called for internal function errors must have
two formal arguments in the following order:

   1.  A single integer containing the error number that is determined by
       the internal function in which the error occurred.

   2.  The result.

If the trap procedure returns normally and has set the error number
(first argument) to zero, a standard message is printed and the program
aborts.

Control-Y Trap Procedure.   

The specified user subroutine is called if you specify CONTROLY in an ON
statement and if you type Control-Y from the terminal while the program
is running.

No parameters are permitted in this trap procedure.

The actions to be taken after a trap occurs are specified by the CALL
procedure.

If you specify CALL procedure for an error condition when an error
occurs, the corresponding trap (if enabled) suppresses output of the
normal error message, transfers control to a user-defined trap procedure,
and passes zero or more parameters describing the error to this
procedure.  This procedure can attempt to analyze or recover from the
error, or can execute another programming path you specify.

Exiting a Trap Procedure 

Upon exit from a trap procedure, control returns to the instruction
following the one that activated the trap.  However, in the case of
external and internal function traps, if the trap procedure returns
normally and has set the error number (first argument) to zero, a
standard message is printed and the program aborts.



MPE/iX 5.0 Documentation