Example 17 Example5-4 call_itrap.f90

PROGRAM main

!$HP$ CHECK_OVERFLOW INTEGER ON

INTEGER :: i ON INTEGER OVERFLOW CALL trap_oflow

!assign to i the biggest number it can hold I = 2147483647

!now add 1

I = i + 1 PRINT *, i END PROGRAM main SUBROUTINE trap_oflow(n) INTEGER :: n

! write error message to standard error

WRITE (7, *) 'integer overflow occurred, assigning 0 to result' n = 0

END SUBROUTINE trap_oflow

If you were to comment out the ON statement but keep the directive, the program would abort with a procedure traceback and a core dump. Compiling with the +check=alloption would have the same effect.

Trapping +Ctrl-C trap interrupts

A +Ctrl-Cinterrupt can occur during the following circumstances:

When the user enters the interrupt code from the terminal while the program is running or awaiting input

During the execution of a PAUSEstatement

The trap procedure for a +Ctrl-Cinterrupt must have no formal arguments. The interrupt code is the character defined by the HP-UX stty(1)command for the intrparameter. The system default for intris +Ctrl-C.

You can use the +Ctrl-Cform of the ONstatement to handle the interrupt signal 2. In the following example, when an interrupt occurs, the program reports status information on standard output, assuring the user that the program is still at work in the DOloop. The program uses the ONstatement to set the action for a +Ctrl-Cinterrupt to be the call to the trap handler status:

PROGRAM main COMMON i

ON CONTROLC CALL status DO i = 1, 100000

...! Long computation END DO END

SUBROUTINE status COMMON i

PRINT *, 'Currently on iteration ', i END SUBROUTINE status

When this program is run, a +Ctrl-Cinterrupt causes the status routine to be called, which prints the iteration count. The program then resumes executing the DOloop.

Allowing core dumps

If a program includes the ONstatement and takes an exception other than the one specified by the exception keywords, the program will abort with a procedure traceback but without a core dump. If you want to allow a core dump for one or more signals for a program that includes the ONstatement, you must revise the program for each such signal.

For example, you may wish to handle floating-point exceptions with the ONstatement, but still allow a core dump for other signals (for example, a bus error). The following example program uses theSIGNAL routine in the libU77library to reset the default behavior for a bus error signal. The

Trapping +Ctrl-C trap interrupts

85