Created
February 13, 2019 18:34
-
-
Save certik/bd8235d2e1d049b22fcd1016f4914430 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 | |
call mysum(5, s, stat) | |
if (stat /= 0) error stop | |
print *, s | |
call mysum(5, s) | |
print *, s | |
contains | |
subroutine mysum(n, s, stat) | |
integer, intent(in) :: n | |
integer, intent(out), optional :: stat | |
integer, intent(out) :: s | |
integer, allocatable :: A(:) | |
integer :: i | |
call myallocate(n, A, stat=stat) | |
! We must check 'stat' by hand here and exit immediately: | |
if (present(stat)) then | |
if (stat /= 0) return | |
end if | |
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 :: stat | |
integer, allocatable, intent(out) :: A(:) | |
integer :: mystat | |
if (present(stat)) then | |
allocate(A(n), stat=stat) | |
else | |
allocate(A(n)) | |
end if | |
end subroutine | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment