Skip to content

Instantly share code, notes, and snippets.

@certik
Created February 13, 2019 18:34
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/d950b7468228ff6f7d8bbc680a0943a7 to your computer and use it in GitHub Desktop.
Save certik/d950b7468228ff6f7d8bbc680a0943a7 to your computer and use it in GitHub Desktop.
program exceptions
implicit none
integer :: s, stat
call mysum(5, s, stat)
if (stat /= 0) error stop
print *, s
! Equivalent to the above
try mysum(5, s, stat)
print *, s
! This will fail in 'allocate' below
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
try myallocate(n, A, stat=stat)
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
allocate(A(n), stat=stat)
end subroutine
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment