Why doesn't tryCatch return any warnings when asked to produce them?

Check out the following example:

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!

      

Why does the test say there were no warnings?

Using the function withWarnings

discussed here
also doesn't show any warnings. Why tryCatch

doesn't it display a warning if asked?

+3


source to share


1 answer


You have created nested calls doTryCatch

and withCallingHandlers

. The problem is that e

it is not a symbol, but an object simpleError

(which contains another call doTryCatch

). The following several work, but show the actual context in which the warning was raised:



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

      

+2


source







All Articles