error stop

Contents
Stop with message
Exit codes
Assert subroutine
Guard clauses

Stop with message

Error stop ends the program and prints your message to the error unit. Use it for faults the program cannot fix, such as bad input sizes. The message should say what was wrong and which value was seen.

program stop_demo implicit none integer :: n = -1 if (n <= 0) error stop "n must be positive" print *, n end program stop_demo

Exit codes

An integer code lets scripts tell failures apart. Zero means success, so nonzero codes mark each error path. Pick one code per failure and write the mapping in a comment.

program code_demo implicit none integer :: ios, n ! 2 means bad input read(*, *, iostat=ios) n if (ios /= 0) error stop 2 print *, n end program code_demo

Assert subroutine

A tiny assert subroutine turns every assumption into one line. Call it with a condition and a message after each risky step. Failed checks stop at once with the message attached.

subroutine assert(ok, msg) implicit none logical, intent(in) :: ok character(len=*), intent(in) :: msg if (.not. ok) error stop msg end subroutine assert

Guard clauses

Guard clauses test arguments at the top of a procedure and stop bad calls early. The main logic below then stays flat and readable. Each guard names the routine so the message points at the culprit.

subroutine set_n(n) implicit none integer, intent(in) :: n if (n <= 0) error stop "set_n: n must be positive" print *, n end subroutine set_n

Next: Testing

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: