This example demonstrates how to handle errors using the iostat specifier in Fortran.
fortranCopy codeprogram error_handling
implicit none
integer :: i, ios
real :: value
open(unit=10, file='data.txt', status='old', iostat=ios)
if (ios /= 0) then
print *, 'Error opening file!'
stop
end if
do
read(10, *, iostat=ios) value
if (ios /= 0) exit
print *, 'Read value: ', value
end do
close(10)
end program error_handling
Error Handling with iostat