为什么tryCatch在被要求生成警告时不返回警告?

为什么tryCatch在被要求生成警告时不返回警告?,r,testthat,R,Testthat,检查以下示例: 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), na

检查以下示例:

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!
为什么测试表明没有警告


使用也不会显示任何警告迹象。为什么
tryCatch
在被请求时不会产生警告?

您创建了对
doTryCatch
的嵌套调用,调用了callinghandlers
。问题在于
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