Skip to content

Instantly share code, notes, and snippets.

@certik
Last active May 14, 2020
Embed
What would you like to do?
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