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()