Skip to content

Commit 6661975

Browse files
authored
Merge pull request #166 from jvdp1/spec_check
Spec for error_stop and check
2 parents e537708 + 46309cb commit 6661975

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

src/stdlib_experimental_error.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Catching and handling errors
2+
3+
* [`check` - Checks the value of a logical condition](#check---checks-the-value-of-a-logical-condition)
4+
* [`error_stop` - aborts the program](#error_stop---aborts-the-program)
5+
6+
7+
## `check` - Checks the value of a logical condition
8+
9+
### Description
10+
11+
Checks the value of a logical condition.
12+
13+
### Syntax
14+
15+
`call check(condition, msg, code, warn)`
16+
17+
### Arguments
18+
19+
`condition`: Shall be a scalar of type `logical`.
20+
21+
`msg` (optional): Shall be a character expression containing the message to be printed to `stderr`. The default `msg` is 'Check failed.'.
22+
23+
`code` (optional): Shall be a scalar of type `integer`. The default `code` is `1`.
24+
25+
`warn` (optional): Shall be a scalar of type `logical`. The default `warn` is `.true.`.
26+
27+
### Return value
28+
29+
If `condition` is `.false`., and:
30+
31+
* no other arguments are provided, this subroutine stops the program with the default message and exit code 1;
32+
33+
* `msg` is provided, this subroutine stops the program and it prints the value of `msg`;
34+
35+
* `code` is provided, this subroutine stops the program with the given exit code;
36+
37+
* `warn` is provided and `warn` is `.true.`, this subroutine doesn't stop the program and prints the message.
38+
39+
### Examples
40+
41+
```fortran
42+
program demo_check1
43+
use stdlib_experimental_error, only: check
44+
implicit none
45+
integer :: a = 1
46+
! If a /= 5, stops the program with exit code 1 and prints 'Check failed.'
47+
call check(a == 5)
48+
end program demo_check1
49+
```
50+
```fortran
51+
program demo_check2
52+
use stdlib_experimental_error, only: check
53+
implicit none
54+
integer :: a = 1
55+
! If a /= 5, stops the program with exit code 1 and prints 'a == 5 failed.'
56+
call check(a == 5, msg='a == 5 failed.')
57+
end program demo_check2
58+
```
59+
```fortran
60+
program demo_check3
61+
use stdlib_experimental_error, only: check
62+
implicit none
63+
integer :: a = 1
64+
! If a /= 5, prints 'a == 5 failed.', but doesn't stop the program.
65+
call check(a == 5, msg='a == 5 failed.', warn=.true.)
66+
end program demo_check2
67+
```
68+
```fortran
69+
program demo_check3
70+
use stdlib_experimental_error, only: check
71+
implicit none
72+
integer :: a = 1
73+
! If a /= 5, stops the program with exit code 77 and prints 'a == 5 failed.'
74+
call check(a == 5, msg='a == 5 failed.', code=77)
75+
end program demo_check3
76+
```
77+
78+
## `error_stop` - aborts the program
79+
80+
### Description
81+
82+
Aborts the program with a message and a nonzero exit code.
83+
84+
### Syntax
85+
86+
`call error_stop(msg, code)`
87+
88+
### Arguments
89+
90+
`msg`: Shall be a character expression containing the message to be printed to `stderr`.
91+
92+
`code` (optional): Shall be a scalar of type `integer` to be returned as exit code.
93+
94+
### Output
95+
96+
Aborts the program with printing the message `msg` to `stderr` and a nonzero exit code. The nonzero exit code is equal to `code` if provided, and 1 otherwise.
97+
98+
### Examples
99+
100+
Without error code:
101+
102+
```fortran
103+
program demo_error_stop1
104+
use stdlib_experimental_error, only: error_stop
105+
implicit none
106+
call error_stop("Invalid argument")
107+
end program demo_error_stop1
108+
```
109+
110+
With error code:
111+
112+
```fortran
113+
program demo_error_stop2
114+
use stdlib_experimental_error, only: error_stop
115+
implicit none
116+
call error_stop("Invalid argument", code = 123)
117+
end program demo_error_stop2
118+
```

0 commit comments

Comments
 (0)