Panic in Co-array Fortran
Running this program will cause it to terminate with an error, print an error message, and exit with a non-zero status.
When the first error stop
in main
executes, the program exits without reaching the rest of the code. If you’d like to see the program try to create a temp file, comment out the first error stop
.
Note that unlike some languages which use exceptions for handling of many errors, in Fortran it is idiomatic to use error codes and status variables for error handling wherever possible.
In Fortran, there isn’t a direct equivalent to Go’s panic
function. Instead, we use error stop
to immediately terminate the program with an error message. This serves a similar purpose to panic
in Go, allowing us to halt execution when an unexpected error occurs.
The open
statement in Fortran is used to create or open files, similar to os.Create
in Go. We use the iostat
and iomsg
specifiers to capture any errors that occur during the file operation.
Remember that Co-array Fortran is an extension of Fortran that adds parallel processing capabilities. In this example, we’re not using any Co-array specific features, but in more complex programs, you might use co-arrays for parallel execution across multiple images.