Terminating program execution

Use the ABORTform of the CALLstatement to terminate the program when an exception occurs. In the following example, the log is taken of a negative number. The ABORTclause causes the program immediately after the exception is detected and to issue a procedure traceback:

Example 14 Example5-1 abort.f90

PROGRAM main

REAL :: x, y, z

!The next statement enables traps for floating-point exceptions

!and specifies the action to take for divide by zero.

!ON REAL DIV 0 ABORT

x= 10.0

y = 0.0

z = x / y PRINT *, y END PROGRAM main

Here is the command line and the output from a sample run:

$ f90 abort.f90 $ a.out

PROGRAM ABORTED : IEEE divide by zero

PROCEDURE TRACEBACK:

( 0) 0x0000248c _start + 0x6c [./a.out]

The program would have the same result if you were to comment out the ON statement and compile with the +fp_exceptionoption.

Ignoring errors

You can use the ONstatement to ignore an exception by specifying the IGNOREkeyword. The following paragraphs discuss an example program,ignore.f90, that uses the ONstatement to ignore an invalid operation. The following program illustrates this.

Example 15 Example5-2 ignore.f90

PROGRAM main REAL :: x, y, z

!The following ON statement enables traps for floating-point

!exceptions and causes the program to ignore an invalid

!operation exception. ON REAL ILLEGAL IGNORE

!The next two statements pass a negative argument to the LOG

!intrinsic, resulting in an invalid operation. This

!exception is ignored, as specified by the ON statement. x = -10.0 y = LOG(x)

PRINT *, y

!The next three statements attempt to divide by zero. The

!trap for this exception is enabled by the previous

!ON statement but no action is specified. Therefore,

!the program will abort execution.

x= 9.0 y = 0 z = x/y

PRINT *, z

END PROGRAM main

As defined by the IEEE standard, a floating-point operation that results in a NaN is an exception known as an invalid operation. The example program performs an invalid operation when it passes a negative argument to the LOG intrinsic, causing the intrinsic to return a NaN. The following ON statement:

ON REAL INVALID IGNORE

causes the program to ignore this exception and continue execution.

Actions specified by ON 83