【问题标题】:How to catch errors in elua (namely NodeMCU)如何在 elua(即 NodeMCU)中捕获错误
【发布时间】:2017-09-25 06:30:49
【问题描述】:

假设我正在导入一些东西:

t = require("ds18b20")
t.setup(1)
temperatura = t.read()

如何捕捉“导入失败”之类的错误?

执行 pcall(t.setup(1)) 之类的操作只会返回语法错误。

【问题讨论】:

    标签: lua try-catch nodemcu


    【解决方案1】:

    如果错误是由require引发的,没有找到ds18b20,那么你可以这样做

    ok, t = pcall(require, "ds18b20")
    if not ok then
      -- handle error, t has error message
    else
      -- can use t
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-08
      • 2018-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-08
      • 2010-11-21
      • 2019-03-27
      相关资源
      最近更新 更多