ERROR STOP (Fortran 2008)
用途
ERROR STOP 语句启动程序的错误终止,这将终止程序的执行。 如果指定了 stop_code,则会将关键字“ERROR STOP”(后跟 stop_code)打印到 ERROR_UNIT。
语法
规则
执行 ERROR STOP 语句时,将提供一个系统返回码并将一条错误消息打印到 ERROR_UNIT,具体取决于是否指定了 stop_code:
- 如果 stop_code 为 scalar_char_constant_expr,那么系统返回码为 1。 打印关键字“ERROR STOP”,后跟 stop_code。
- 如果 stop_code 为 scalar-int-constant-expr,那么 IBM® Open XL Fortran 会将系统返回码设置为 MOD (stop_code, 256)。 打印关键字“ERROR STOP”,后跟 stop_code。
- 如果未指定任何内容,那么系统返回码为 1。 不会打印错误消息。
不能使用 ERROR STOP 语句作为终止 DO 构造的带标签语句。
示例
以下示例显示如何使用 ERROR STOP 语句:
PROGRAM p
INTEGER, SAVE :: s = -1
INTEGER, SAVE :: arr(3) = -1
! If the initialization for s is wrong, the error
! message "ERROR STOP Initial value wrong!" is printed.
! The system return code is 1.
IF (s .NE. -1) ERROR STOP "Initial value wrong!"
! If the initialization for arr is wrong, no message is printed.
! The system return code is 1.
IF (ANY(arr .NE. -1)) ERROR STOP
s = 1
arr = 1
! If the value for s is not 1, the error message "ERROR STOP 127" is printed.
! The system return code is 127.
IF (s .NE. 1) ERROR STOP 127
! If the value for arr is not 1, the error message "ERROR STOP 0" is printed.
! The system return code is 0.
IF (ANY(arr .NE. 1)) ERROR STOP 0
STOP "Good!"
END PROGRAM p
