Skip to content

Exceptions not cleared if except control flow is interrupted #25031

@yglukhov

Description

@yglukhov

Nim Version

Nim Compiler Version 2.3.1 [Linux: amd64]
Compiled at 2025-07-07
Copyright (c) 2006-2025 by Andreas Rumpf

git hash: fbdc9a4c19aafc25937aaa51f5c1f01084094688
active boot switches: -d:release

Description

proc foo() =
  try:
    try:
      raise newException(Exception, "1")
    except:
      echo "EXCEPT1: ", getCurrentExceptionMsg()
      return # Prevents exception clearing
      # raise newException(Exception, "2") # also prevents clearing
    finally:
      echo "FINALLY: ", getCurrentExceptionMsg()
  except:
    echo "EXCEPT2:", getCurrentExceptionMsg()
  finally:
    echo "FINALLY2: ", getCurrentExceptionMsg()

  echo "LAST: ", getCurrentExceptionMsg()

foo()

Current Output

EXCEPT1: 1
FINALLY1: 1
FINALLY2: 1

Expected Output

EXCEPT1: 1
FINALLY1:
FINALLY2:

Known Workarounds

No response

Additional Information

While playing with closureiter transformation I realized I'm not accounting for this case, so I went to see how it's handled in regular code, and it turned out it isn't.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions