Except clauses
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 CatchableError:
echo getCurrentExceptionMsg()
当前内容版权归 nim-lang.org 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 nim-lang.org .