【问题标题】:Why tryCatch returns no warnings when it was asked for producing them?为什么 tryCatch 在被要求生成警告时不返回警告?
【发布时间】:2017-07-18 22:31:12
【问题描述】:

检查以下示例:

library(testthat)
expect_warning(tryCatch(stop("Error!"), error = function(e) warning(e)))
## Error: tryCatch(stop("Error!"), error = function(e) warning(e)) showed 0 warnings
## In addition: Warning message:
## In doTryCatch(return(expr), name, parentenv, handler) : Error!

为什么 testthat 说没有警告?

使用withWarnings function discussed in here 也没有任何警告迹象。为什么tryCatch 在被要求时不会产生警告?

【问题讨论】:

    标签: r testthat


    【解决方案1】:

    您创建了对doTryCatch 和withCallingHandlers 的嵌套调用。问题是e 不是一个字符,而是一个simpleError 对象(其中包含对doTryCatch 的另一个调用)。以下内容有些作用,但显示了引发警告的实际上下文:

    tryCatch(stop("Error!"), error = function(e) warning(e[[1]]))
    #Warning message:
    #In value[[3L]](cond) : Error!
    library(testthat)
    expect_warning(tryCatch(stop("Error!"), error = function(e) warning(e[[1]])))
    #no further output
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      • 2013-07-16
      • 2023-03-05
      • 1970-01-01
      • 2017-02-25
      • 1970-01-01
      相关资源
      最近更新 更多