Recover in Co-array Fortran
Co-array Fortran doesn’t have built-in panic and recover mechanisms like some other languages. However, we can simulate similar behavior using error handling and external command execution.
In this example, we define two subroutines:
may_panic()
: This subroutine simulates a panic by usingerror stop
with an error message.main()
: This is our main subroutine where we handle the potential error.
The main()
subroutine uses execute_command_line
to run may_panic()
as an external command. This allows us to catch any errors that occur during its execution.
If an error occurs (i.e., error_status /= 0
), we print a “Recovered” message along with the error details. This simulates the recovery process.
The line print *, "After may_panic()"
will always be executed in this case, unlike in some other languages where code after a panic might not run. This is because we’re using external command execution to simulate the panic and recover behavior.
To run this program:
- Save the
may_panic
subroutine as a separate executable. - Compile and run the main program:
This approach doesn’t provide the same level of control as languages with built-in panic and recover mechanisms, but it demonstrates a way to handle and recover from errors in Co-array Fortran.