|
1 | 1 | module stdlib_experimental_error
|
| 2 | +use, intrinsic :: iso_fortran_env, only: stderr=>error_unit |
2 | 3 | implicit none
|
3 | 4 | private
|
| 5 | + |
| 6 | +interface ! f{08,18}estop.f90 |
| 7 | +module subroutine error_stop(msg, code) |
| 8 | +character(*), intent(in) :: msg |
| 9 | +integer, intent(in), optional :: code |
| 10 | +end subroutine error_stop |
| 11 | +end interface |
| 12 | + |
4 | 13 | public :: assert, error_stop
|
5 | 14 |
|
6 | 15 | contains
|
7 | 16 |
|
8 |
| -subroutine assert(condition) |
| 17 | +subroutine assert(condition, code) |
9 | 18 | ! If condition == .false., it aborts the program.
|
10 | 19 | !
|
11 | 20 | ! Arguments
|
12 | 21 | ! ---------
|
13 | 22 | !
|
14 | 23 | logical, intent(in) :: condition
|
| 24 | +integer, intent(in), optional :: code |
15 | 25 | !
|
16 | 26 | ! Example
|
17 | 27 | ! -------
|
18 | 28 | !
|
19 | 29 | ! call assert(a == 5)
|
20 | 30 |
|
21 |
| -if (.not. condition) call error_stop("Assert failed.") |
22 |
| -end subroutine |
23 |
| - |
24 |
| -subroutine error_stop(msg) |
25 |
| -! Aborts the program with nonzero exit code |
26 |
| -! |
27 |
| -! The statement "stop msg" will return 0 exit code when compiled using |
28 |
| -! gfortran. error_stop() uses the statement "stop 1" which returns an exit code |
29 |
| -! 1 and a print statement to print the message. |
30 |
| -! |
31 |
| -! Example |
32 |
| -! ------- |
33 |
| -! |
34 |
| -! call error_stop("Invalid argument") |
35 |
| - |
36 |
| -character(len=*) :: msg ! Message to print on stdout |
37 |
| -print *, msg |
38 |
| -stop 1 |
| 31 | +if (.not. condition) call error_stop("Assert failed.", code) |
39 | 32 | end subroutine
|
40 | 33 |
|
41 | 34 | end module
|
0 commit comments