Last active
May 14, 2020 03:27
-
-
Save certik/012ed004257e14fc2a7557c7943eeaf1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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