Skip to content
Prev 1368 / 12125 Next

[R-pkg-devel] tryCatch defensive programming guidance

Hi Glenn,


Better late than never:

couldn't you simply use try?


result <- try(  log("a")  )


The printing is horrible: people will think an error occured (but the function didn't stop!)

I tend to use it like this (which may be totally unintended):



res <- try(log("a"), silent=TRUE)
if(inherits(res, "try-error"))
{
 message("log failed: ",res,". Now continuing with res=0.")
 res <- 0
}




See here for my version that captures errors/warnings/messages with call tracing:

https://www.rdocumentation.org/packages/berryFunctions/topics/tryStack


Regards,

Berry