3

Check 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 testthat say that there was no warnings?

Using the withWarnings function discussed in here also shows no sign of warnings. Why tryCatch does not produce warnings if it was asked for it?

Tim
  • 7,075
  • 6
  • 29
  • 58

1 Answers1

2

You created nested calls to doTryCatch and withCallingHandlers. The problem is that e is not a character, but a simpleError object (which contains another call to doTryCatch). The following somewhat works, but shows the actual context where the warning was thrown:

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
Roland
  • 127,288
  • 10
  • 191
  • 288