Error Handling Patterns
Guarded Divide
Return a Status
A guard checks the risky condition before doing the work. The caller can inspect a status value and keep control flow explicit.
Program
Play the program to choose a divisor and watch the guard choose a result and status.
guarded_divide.f90
Replay: real traced execution (multi-file project)
program guarded_divide_demo
implicit none
integer :: numerator, divisor
integer :: quotient, status
numerator = 20
divisor = 2
if (divisor == 0) then
quotient = 0
status = 1
else
quotient = numerator / divisor
status = 0
end if
print '(I0, 1X, I0)', quotient, status
end program guarded_divide_demo
program guarded_divide_demo
implicit none
integer :: numerator, divisor
integer :: quotient, status
numerator = 20
divisor = 0
if (divisor == 0) then
quotient = 0
status = 1
else
quotient = numerator / divisor
status = 0
end if
print '(I0, 1X, I0)', quotient, status
end program guarded_divide_demo
program guarded_divide_demo
implicit none
integer :: numerator, divisor
integer :: quotient, status
numerator = 20
divisor = 5
if (divisor == 0) then
quotient = 0
status = 1
else
quotient = numerator / divisor
status = 0
end if
print '(I0, 1X, I0)', quotient, status
end program guarded_divide_demo
numerator ← 20
6numerator = 207divisor = 2values this step20numeratordivisor ← 2
6numerator = 207divisor = 28if (divisor == 0) thenvalues this step2divisordivisor == 0 ← .false.
7divisor = 28if (divisor == 0) then9 quotient = 0values this step.false.divisor == 02divisorquotient ← 10
11else12 quotient = numerator / divisor13 status = 0values this step10quotient20numerator2divisorstatus ← 0
12 quotient = numerator / divisor13 status = 014end ifvalues this step0statusprint '(I0, 1X, I0)', quotient, status
14 end if15 print '(I0, 1X, I0)', quotient, status16end program guarded_divide_demooutput10 0values this step10quotient0status
numerator ← 20
6numerator = 207divisor = 0values this step20numeratordivisor ← 0
6numerator = 207divisor = 08if (divisor == 0) thenvalues this step0divisordivisor == 0 ← .true.
7divisor = 08if (divisor == 0) then9 quotient = 0values this step.true.divisor == 00divisorquotient ← 0
8if (divisor == 0) then9 quotient = 010 status = 1values this step0quotientstatus ← 1
9 quotient = 010 status = 111elsevalues this step1statusprint '(I0, 1X, I0)', quotient, status
14 end if15 print '(I0, 1X, I0)', quotient, status16end program guarded_divide_demooutput0 1values this step0quotient1status
numerator ← 20
6numerator = 207divisor = 5values this step20numeratordivisor ← 5
6numerator = 207divisor = 58if (divisor == 0) thenvalues this step5divisordivisor == 0 ← .false.
7divisor = 58if (divisor == 0) then9 quotient = 0values this step.false.divisor == 05divisorquotient ← 4
11else12 quotient = numerator / divisor13 status = 0values this step4quotient20numerator5divisorstatus ← 0
12 quotient = numerator / divisor13 status = 014end ifvalues this step0statusprint '(I0, 1X, I0)', quotient, status
14 end if15 print '(I0, 1X, I0)', quotient, status16end program guarded_divide_demooutput4 0values this step4quotient0status
guard
The `if` condition checks for the unsafe divisor before division.
status
`status = 1` records the failure path without stopping the program.
normal path
When the guard is false, the program computes the quotient normally.