错误

stdlib_error 模块

简介

捕获和处理错误。

提供的过程和方法

check - 检查逻辑条件的值

状态

实验性

描述

检查逻辑条件的值。

语法

call check (condition, msg, code, warn)

参数

condition:应为类型为 logical 的标量。

msg(可选):应为包含要打印到 stderr 的消息的字符表达式。默认 msg 为 'Check failed.'。

code(可选):应为类型为 integer 的标量。默认 code1

warn(可选):应为类型为 logical 的标量。默认 warn.true.

返回值

如果 condition.false.,并且

  • 没有提供其他参数,此子程序将使用默认消息和退出代码 1 终止程序;

  • 提供了 msg,此子程序将终止程序并打印 msg 的值;

  • 提供了 code,此子程序将使用给定的退出代码终止程序;

  • 提供了 warn 并且 warn.true.,此子程序不会终止程序并打印消息。

示例

program example_check1
  use stdlib_error, only: check
  implicit none
  integer :: a = 1
! If a /= 5, stops the program with exit code 1 and prints 'Check failed.'
  call check(a == 5)
end program example_check1
program example_check2
  use stdlib_error, only: check
  implicit none
  integer :: a = 1
! If a /= 5, stops the program with exit code 1 and prints  'a == 5 failed.'
  call check(a == 5, msg='a == 5 failed.')
end program example_check2
program example_check3
  use stdlib_error, only: check
  implicit none
  integer :: a = 1
! If a /= 5,  prints 'a == 5 failed.', but doesn't stop the program.
  call check(a == 5, msg='a == 5 failed.', warn=.true.)
end program example_check3
program example_check4
  use stdlib_error, only: check
  implicit none
  integer :: a = 1
! If a /= 5, stops the program with exit code 77 and prints 'a == 5 failed.'
  call check(a == 5, msg='a == 5 failed.', code=77)
end program example_check4

error_stop - 终止程序

状态

实验性

描述

使用消息和非零退出代码终止程序。

语法

call error_stop (msg, code)

参数

msg:应为包含要打印到 stderr 的消息的字符表达式。

code(可选):应为要作为退出代码返回的类型为 integer 的标量。

输出

终止程序,并将消息 msg 打印到 stderr 并返回非零退出代码。如果提供了非零退出代码,则其等于 code,否则为 1。

示例

无错误代码

program example_error_stop1
  use stdlib_error, only: error_stop
  implicit none
  call error_stop("Invalid argument")
end program example_error_stop1

有错误代码

program example_error_stop2
  use stdlib_error, only: error_stop
  implicit none
  call error_stop("Invalid argument", code=123)
end program example_error_stop2