如何在Nim中捕获任何错误并获取错误本身

问题描述

我想捕获任何错误获取错误本身

可能会得到特定类型的错误

try:
  raise newException(CatchableError,"some error")
except IOError as e:
  echo e.msg

可能会出现任何错误,但是需要调用特殊函数获取感觉确实错误错误

try:
  raise newException(CatchableError,"some error")
except:
  let e = getCurrentException()
  echo e.msg
 

有没有办法做到这一点?

try:
  raise newException(CatchableError,"some error")
except e:
  echo e.msg

解决方法

任何可捕获的错误都应扩展CatchableError,因此这应该可以满足您的要求:

try:
  raise newException(IOError,"some error")
except CatchableError as e:
  echo e.msg