Try expression
Try can also be used as an expression; the type of the try branch then needs to fit the types of except branches, but the type of the finally branch always has to be void:
from std/strutils import parseInt
let x = try: parseInt("133a")
except ValueError: -1
finally: echo "hi"
To prevent confusing code there is a parsing limitation; if the try follows a ( it has to be written as a one liner:
from std/strutils import parseInt
let x = (try: parseInt("133a") except ValueError: -1)
当前内容版权归 nim-lang.org 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 nim-lang.org .