-
Notifications
You must be signed in to change notification settings - Fork 189
Spec for error_stop and check #166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5728681
spec_check: init
jvdp1 af02310
correction typo
jvdp1 aafebaf
Update src/stdlib_experimental_error.md
jvdp1 d3de2a2
comments from milan
jvdp1 3e0f3f7
Merge branch 'spec_check' of https://github.com/jvdp1/stdlib into spe…
jvdp1 e0b266d
Update src/stdlib_experimental_error.md
jvdp1 634da68
Update src/stdlib_experimental_error.md
jvdp1 6a8a901
spec_check:following suggestion @certik
jvdp1 46309cb
Merge branch 'spec_check' of https://github.com/jvdp1/stdlib into spe…
jvdp1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# Catching and handling errors | ||
|
||
* [`check` - Checks the value of a logical condition](#check---checks-the-value-of-a-logical-condition) | ||
* [`error_stop` - aborts the program](#error_stop---aborts-the-program) | ||
|
||
|
||
## `check` - Checks the value of a logical condition | ||
|
||
### Description | ||
|
||
Checks the value of a logical condition. | ||
|
||
### Syntax | ||
|
||
`call check(condition, msg, code, warn)` | ||
|
||
### Arguments | ||
|
||
`condition`: Shall be a scalar of type `logical`. | ||
|
||
`msg` (optional): Shall be a character expression containing the message to be printed to `stderr`. The default `msg` is 'Check failed.'. | ||
|
||
`code` (optional): Shall be a scalar of type `integer`. The default `code` is `1`. | ||
|
||
`warn` (optional): Shall be a scalar of type `logical`. The default `warn` is `.true.`. | ||
|
||
### Return value | ||
|
||
If `condition` is `.false`., and: | ||
|
||
* no other arguments are provided, this subroutine stops the program with the default message and exit code 1; | ||
|
||
* `msg` is provided, this subroutine stops the program and it prints the value of `msg`; | ||
|
||
* `code` is provided, this subroutine stops the program with the given exit code; | ||
|
||
* `warn` is provided and `warn` is `.true.`, this subroutine doesn't stop the program and prints the message. | ||
|
||
### Examples | ||
|
||
```fortran | ||
program demo_check1 | ||
use stdlib_experimental_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 demo_check1 | ||
``` | ||
```fortran | ||
program demo_check2 | ||
use stdlib_experimental_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 demo_check2 | ||
``` | ||
```fortran | ||
program demo_check3 | ||
use stdlib_experimental_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 demo_check2 | ||
``` | ||
```fortran | ||
program demo_check3 | ||
use stdlib_experimental_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 demo_check3 | ||
``` | ||
|
||
## `error_stop` - aborts the program | ||
|
||
### Description | ||
|
||
Aborts the program with a message and a nonzero exit code. | ||
|
||
### Syntax | ||
|
||
`call error_stop(msg, code)` | ||
|
||
### Arguments | ||
|
||
`msg`: Shall be a character expression containing the message to be printed to `stderr`. | ||
|
||
`code` (optional): Shall be a scalar of type `integer` to be returned as exit code. | ||
|
||
### Output | ||
|
||
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. | ||
|
||
### Example | ||
|
||
```fortran | ||
program demo_error_stop | ||
use stdlib_experimental_error, only: error_stop | ||
implicit none | ||
call error_stop("Invalid argument", code = 123) | ||
end program demo_error_stop | ||
``` | ||
jvdp1 marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.