stdlib_error
模块捕获和处理错误。
check
- 检查逻辑条件的值实验性
检查逻辑条件的值。
call
check (condition, msg, code, warn)
condition
:应为类型为 logical
的标量。
msg
(可选):应为包含要打印到 stderr
的消息的字符表达式。默认 msg
为 'Check failed.'。
code
(可选):应为类型为 integer
的标量。默认 code
为 1
。
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