Skip to content

Instantly share code, notes, and snippets.

@zevv
Last active May 31, 2019 19:54
Show Gist options
  • Save zevv/6beac282e48c3e9258eeb245c703db3e to your computer and use it in GitHub Desktop.
Save zevv/6beac282e48c3e9258eeb245c703db3e to your computer and use it in GitHub Desktop.

Within an except clause it is possible to access the current exception using the following syntax:

  try:
    # ...
  except IOError as e:
    # Now use "e"
    echo "I/O error: " & e.msg

Alternatively, it is possible to use getCurrentException to retrieve the exception that has been raised:

try:
    # ...
  except IOError:
    let e = getCurrentException()
    # Now use "e"

Note that getCurrentException always returns a ref Exception type. If a variable of the proper type is needed (in the example above, IOError), one must convert it explicitly:

try:
    # ...
  except IOError:
    let e = (ref IOError)(getCurrentException())
    # "e" is now of the proper type

However, this is seldom needed. The most common case is to extract an error message from e, and for such situations it is enough to use getCurrentExceptionMsg:

  try:
    # ...
  except:
    echo getCurrentExceptionMsg()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment