【问题标题】:In JavaScript is there an equivelant to Python's "try ... except" feature?在 JavaScript 中是否有相当于 Python 的“try ... except”功能?
【发布时间】:2016-11-01 10:09:33
【问题描述】:

这是我试图用 JavaScript 解决这个问题的 Python 代码:

try:
    float(s) if '.' in s else int(s)
    return True
except ValueError:
    return False

我正在寻找相当于 Python 中“except”的关键字。

【问题讨论】:

  • 您是否费心查看 Javascript 的 try 声明的文档?它会准确地告诉你你需要知道什么。
  • 我改进了语法并使标题更清晰。
  • Javascript Try/Catch的可能重复

标签: javascript


【解决方案1】:

在 javascript 中,我们有 trycatchfinally

try {
   //may throw
} catch(ex) {
   //handle the error, where ex or what ever you choose to call it is your exception reference
} finally {
   //perform this code regardless
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-02
    • 2012-06-18
    • 2023-03-31
    • 1970-01-01
    • 2016-12-16
    • 2017-01-11
    • 2018-03-19
    • 2020-11-23
    相关资源
    最近更新 更多