Skip to content

Instantly share code, notes, and snippets.

Last active May 14, 2020
What would you like to do?
program exceptions
implicit none
integer :: s, stat
! 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)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment