[R-pkg-devel] safely allocate SEXP and handle failure

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Thu Sep 21 16:37:06 CEST 2023


В Thu, 21 Sep 2023 15:10:56 +0200
Jan Gorecki <j.gorecki using wit.edu.pl> пишет:

> Do you by any chance know any examples of using
> R_withCallingErrorHandler? Or could you modify your example to use
> this instead?

Calling handlers are different from exiting handlers established by
tryCatch. Instead of replacing the value of an expression that raised a
condition with the return value of the exiting handler, calling
handlers get an opportunity to clean up or invoke a so-called restart,
but when they return, the execution continues:

withCallingHandlers(stop("fatal"), error = function(cond) {
 writeLines("I'm a calling handler, this is my argument:")
 str(cond)
})
# I'm a calling handler, this is my argument:
# List of 2
#  $ message: chr "fatal"
#  $ call   : language withCallingHandlers(stop("fatal"), ...
#  - attr(*, "class")= chr [1:3] "simpleError" "error" "condition"
# Error in withCallingHandlers(stop("fatal"), error = function(cond) { :
#   fatal

Since setting up restarts will involve calling into R, a complete
solution will involve much more code than R_tryCatchError.

-- 
Best regards,
Ivan



More information about the R-package-devel mailing list