Skip to content

Instantly share code, notes, and snippets.

@certik
Last active May 14, 2020 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save certik/012ed004257e14fc2a7557c7943eeaf1 to your computer and use it in GitHub Desktop.
Save certik/012ed004257e14fc2a7557c7943eeaf1 to your computer and use it in GitHub Desktop.
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