Last active
May 14, 2020 16:10
-
-
Save certik/8dc9f960f7c58a2cc17f7f5b911eb4fa 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 | |
try | |
! Possible failure will be caught in the `catch default` block | |
call mysum(5, s) | |
catch default | |
error stop | |
end try | |
print *, s | |
! The `fallible` subroutine cannot be called outside an error handling context, so this will give a compiler error | |
! if uncommented: | |
!call mysum(5, s) | |
contains | |
fallible subroutine mysum(n, s) | |
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 | |
fallible subroutine myallocate(n, A) | |
integer, intent(in) :: n | |
integer, allocatable, intent(out) :: A(:) | |
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