Skip to content

return escapes finally block without execution #688

@gradha

Description

@gradha

The following example shows how a finally block is never run:

import os, times

proc test() =
  let
    somePath = "missing_file"
    now = getTime()

  finally: somePath.write_file("")

  var m: float
  try:
    m = toSeconds(getLastModificationTime(somePath))
  except EOS:
    echo "Bailing out getting time!"
    return

  echo "file time ", m
  echo "now time ", now

when isMainModule:
  test()

The following terminal session shows how missing_file is never created.

$ nimrod c -r test.nim
...
Bailing out getting time!
$ ./test
Bailing out getting time!
$ ./test
Bailing out getting time!
$ touch missing_file
$ ./test
file time 1.3854746740000000e+09
now time Tue Nov 26 15:04:35 2013
$ 

The expected behaviour of the program is for the finally block to run in all cases creating the file after the first attempt. If the return is removed from the example the finally block is then run, but that defeats the point of having a finally block, doesn't it?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions