Skip to content

Instantly share code, notes, and snippets.

@certik
Last active May 14, 2020 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save certik/8dc9f960f7c58a2cc17f7f5b911eb4fa to your computer and use it in GitHub Desktop.
Save certik/8dc9f960f7c58a2cc17f7f5b911eb4fa to your computer and use it in GitHub Desktop.
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