Skip to content

Instantly share code, notes, and snippets.

@certik
Last active May 14, 2020
Embed
What would you like to do?
program exceptions
use iso_fortran_env, only: Out_of_memory
implicit none
integer :: s
block enable (Out_of_memory)
! Possible failure will be caught in the `handle` block
call mysum(5, s)
print *, s
end block
! Possible failure will cause the program to stop here with a stacktrace at
! runtime, because `mysum` is not called within a `block enable` block
call mysum(5, s)
print *, s
handle Out_of_memory
error stop
contains
subroutine mysum(n, s) enable(Out_of_memory)
integer, intent(in) :: n
integer, intent(out) :: s
integer, allocatable :: A(:)
integer :: i
call myallocate(n, A)
do i = 1, n
A(i) = i
end do
s = sum(A)
end subroutine
subroutine myallocate(n, A) enable(Out_of_memory)
integer, intent(in) :: n
integer, allocatable, intent(out) :: A(:)
! Can raise the Out_of_memory exception
allocate(A(n), failing=.true.)
end subroutine
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment