Skip to content

Finally as statement raises exception in next try/except block #908

@gradha

Description

@gradha

The following example shows an undesired behaviour of finally as a statement:

proc do_stuff(out_filename: string): bool =
  var out_file: TFile
  try:
    if not out_file.open(out_filename, fmWrite):
      echo "Could not open ", out_filename, " for writing."
      return
  except EOutOfMemory:
    return

  finally: out_file.close

  try: out_file.write("yeah\n")
  except EIO:
    echo "Could not write header"
    return

  result = true

when isMainModule:
  echo "Did stuff? ", do_stuff("test.txt")

When run the output is

Could not write header
Did stuff? false

The file test.txt is created but empty. If the finally line is commented out, the example works and the test file gets written to. This suggest that finally forces the next try/except block to fail.

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