Last active
February 18, 2019 20:25
-
-
Save certik/2293270ac39f2589ae702ed751f405f8 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 | |
implicit none | |
integer :: s, stat_manual | |
integer, status :: stat | |
call mysum(5, s, stat_manual) | |
if (stat_manual /= 0) error stop | |
print *, s | |
! Equivalent to the above | |
call mysum(5, s, stat) | |
print *, s | |
! This will fail in 'allocate' below | |
call mysum(5, s) | |
print *, s | |
contains | |
subroutine mysum(n, s, stat) | |
integer, intent(in) :: n | |
integer, intent(out), optional, status :: stat | |
integer, intent(out) :: s | |
integer, allocatable :: A(:) | |
integer :: i | |
call myallocate(n, A, stat=stat) | |
do i = 1, n | |
A(i) = i | |
end do | |
s = sum(A) | |
end subroutine | |
subroutine myallocate(n, A, stat) | |
integer, intent(in) :: n | |
integer, intent(out), optional, status :: stat | |
integer, allocatable, intent(out) :: A(:) | |
integer :: mystat | |
! This assumes that `allocate` works with the STATUS attribute | |
allocate(A(n), stat=stat) | |
end subroutine | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment