Errors in Fortran
In Fortran, error handling is typically done through the use of error codes or status variables. While Fortran doesn’t have a built-in error type like Go, we can simulate similar behavior using integer status codes and custom error messages.
In this Fortran version:
We define an
error_module
to encapsulate error-related functionality, similar to Go’serrors
package.Instead of returning multiple values, Fortran subroutines use
intent(out)
parameters to return results and error codes.We use integer error codes instead of error objects. A function
get_error_message
is provided to convert error codes to human-readable messages.The
select case
construct is used instead ofif-else
chains for error handling.Fortran doesn’t have a built-in way to wrap errors, so we simply return the most specific error code.
The
cycle
statement in Fortran is equivalent to Go’scontinue
.
To run this program, save it as errors.f90
and compile it using a Fortran compiler:
This Fortran implementation demonstrates error handling patterns similar to those used in the original Go code, adapted to Fortran’s language features and idioms.