Nim Version
nim --version
Nim Compiler Version 2.3.1 [Linux: amd64]
Compiled at 2025-12-03
Copyright (c) 2006-2025 by Andreas Rumpf
git hash: 2d0b62a
active boot switches: -d:release
Description
Closure iterator swallows finally body when it contains try..except or try..finally:
proc read() =
if true:
raise newException(ValueError, "foo")
iterator count1(): int {.closure.} =
yield 1
read()
iterator count0(): int {.closure.} =
try:
var count = count1
while true:
yield count()
if finished(count): break
finally:
try:
echo "try..except"
var count2 = count1
while true:
yield count2()
if finished(count2): break
discard # removing this outputs "raise"
except:
echo "raise"
raise
proc main =
for x in count0(): echo x
main()
Current Output
1
Error: unhandled exception: foo [ValueError]
Expected Output
1
try..except
1
raise
Error: unhandled exception: foo [ValueError]
Known Workarounds
No response
Additional Information
it works in Nim 2.2.4.