The presentation on Modernizing Legacy Fortran codes by R. Bader linked in a recent post, suggests the following idiom for dealing with exceptions:
integer :: stat
comp: block
real :: x
x = …
call gam(x, stat)
if (stat /= 0) exit comp
! :
end block comp
select case (stat)
case(0)
stop 0
default
write(*,*) 'error:', stat
error stop 1
end select
The “exception handling” occurs after the named block (here rather unimaginative, as the author remarked himself).
The Odin programming language also uses this idiom and does not to include an exception mechanism. In the Q&A section they state:
Why does Odin not have exceptions?
Coupling exceptions to a control structure, as in the try-catch-finally idiom, complicates the understanding of the program.
Odin uses plain error handling through the use of multiple return values. It is clear which procedure the error value is from compared to a
try-catch
approach which is akin to the COMEFROM statement.Please see gingerBill’s article for more information: Exceptions — And Why Odin Will Never Have Them.
The linked article by gingerBill, gives the following example of exception handling in Odin:
Error :: union {
ValueError,
BarError,
BazError,
PlopError,
}
foo :: proc() -> (Value_Type, Error) { ... }
x, err := foo();
switch e in err {
case ValueError:
// Handle error
case BarError:
// Handle error
case BazError, PlopError:
// Handle errors
}
I just found this similarity interesting, and wanted to save the links for future reference. In case you are interested in exception handling in Fortran, there are several past related threads:
- Error handling landscape (March 2023)
- Exceptions proposal (Feb 2023)
- Modernising old libraries - error handling (March 2022)
- ErrorFx: Fortran exception library (October 2021)
- An experiment with exceptions (June 2021)
- Error handling approaches (April 2021)