Except clauses

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

  1. try:
  2. # ...
  3. except IOError as e:
  4. # Now use "e"
  5. echo "I/O error: " & e.msg

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

  1. try:
  2. # ...
  3. except IOError:
  4. let e = getCurrentException()
  5. # 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:

  1. try:
  2. # ...
  3. except IOError:
  4. let e = (ref IOError)(getCurrentException())
  5. # "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:

  1. try:
  2. # ...
  3. except:
  4. echo getCurrentExceptionMsg()