11module stdlib_experimental_error
2- ! !Provides a support for catching and handling errors
2+ ! ! Provide support for catching and handling errors ([spec](../page/specs/stdlib_experimental_error.html))
3+ ! !
4+ ! ! __Read the [specification here](../page/specs/stdlib_experimental_error.html).__
35use , intrinsic :: iso_fortran_env, only: stderr = > error_unit
46use stdlib_experimental_optval, only: optval
57implicit none
68private
79
810interface ! f{08,18}estop.f90
911 module subroutine error_stop (msg , code )
12+ ! ! Provides a call to `error stop` and allows the user to specify a code and message.
13+ ! !
14+ ! ! __Read the [specification here](..//page/specs/stdlib_experimental_error.html#description_1).__
1015 character (* ), intent (in ) :: msg
1116 integer , intent (in ), optional :: code
1217 end subroutine error_stop
@@ -17,15 +22,44 @@ end subroutine error_stop
1722contains
1823
1924subroutine check (condition , msg , code , warn )
25+ ! ! Checks the value of a logical condition. ([spec](../page/specs/stdlib_experimental_error.html#description))
26+ ! !
27+ ! ! __Read the [specification here](../page/specs/stdlib_experimental_error.html#description).__
28+ ! !
29+ ! !##### Behavior
30+ ! !
31+ ! ! If `condition == .false.` and:
32+ ! !
33+ ! ! * No other arguments are provided, it stops the program with the default
34+ ! ! message and exit code `1`;
35+ ! ! * `msg` is provided, it prints the value of `msg`;
36+ ! ! * `code` is provided, it stops the program with the given exit code;
37+ ! ! * `warn` is provided and `.true.`, it doesn't stop the program and prints
38+ ! ! the message.
39+ ! !
40+ ! !##### Examples
41+ ! !
42+ ! !* If `a /= 5`, stops the program with exit code `1`
43+ ! ! and prints `Check failed.`
44+ ! !``` fortran
45+ ! ! call check(a == 5)
46+ ! !```
47+ ! !
48+ ! !* As above, but prints `a == 5 failed`.
49+ ! !``` fortran
50+ ! ! call check(a == 5, msg='a == 5 failed.')
51+ ! !```
52+ ! !
53+ ! !* As above, but doesn't stop the program.
54+ ! !``` fortran
55+ ! ! call check(a == 5, msg='a == 5 failed.', warn=.true.)
56+ ! !```
57+ ! !
58+ ! !* As example #2, but stops the program with exit code `77`
59+ ! !``` fortran
60+ ! ! call check(a == 5, msg='a == 5 failed.', code=77)
61+ ! !```
2062
21- ! Checks the value of a logical condition. If condition == .false. and:
22- !
23- ! * No other arguments are provided, it stops the program with the default
24- ! message and exit code 1;
25- ! * msg is provided, it prints the value of msg;
26- ! * code is provided, it stops the program with the given exit code;
27- ! * warn is provided and .true., it doesn't stop the program and prints
28- ! * the message.
2963 !
3064 ! Arguments
3165 ! ---------
@@ -36,22 +70,6 @@ subroutine check(condition, msg, code, warn)
3670 logical , intent (in ), optional :: warn
3771 character (* ), parameter :: msg_default = ' Check failed.'
3872
39- ! Examples
40- ! --------
41- !
42- ! ! If a /= 5, stops the program with exit code 1
43- ! ! and prints 'Check failed.'
44- ! call check(a == 5)
45- !
46- ! ! As above, but prints 'a == 5 failed.'
47- ! call check(a == 5, msg='a == 5 failed.')
48- !
49- ! ! As above, but doesn't stop the program.
50- ! call check(a == 5, msg='a == 5 failed.', warn=.true.)
51- !
52- ! ! As example #2, but stops the program with exit code 77
53- ! call check(a == 5, msg='a == 5 failed.', code=77)
54-
5573 if (.not. condition) then
5674 if (optval(warn, .false. )) then
5775 write (stderr,* ) optval(msg, msg_default)
0 commit comments