Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why tryCatch returns no warnings when it was asked for producing them?

Tags:

r

testthat

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?

like image 610
Tim Avatar asked Dec 20 '25 14:12

Tim


1 Answers

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
like image 120
Roland Avatar answered Dec 23 '25 05:12

Roland