It seems that withCallingHandlers
doesn't actually catch the error the way that tryCatch
does and the script still stops executing.
Compare the snippet with tryCatch
where both "before" and "after" are printed:
f1 <- function() {
cat("before tryCatch\n")
tryCatch({
stop("this is an error!")
},
error = function(cond) {
print(cond$message)
}
)
cat("after tryCatch\n")
}
With the same snippet with withCallingHandlers
that doesn't print "after" and stops execution:
f2 <- function() {
cat("before tryCatch\n")
withCallingHandlers({
stop("this is an error!")
},
error = function(cond) {
print(cond$message)
}
)
cat("after tryCatch\n")
}
What am I doing wrong?
Some context
I'd like to use withCallingHandlers
to analyze the call stack at the point where the error occurs using sys.calls()
.
According to Advanced R that should be possible:
The handlers in
withCallingHandlers()
are called in the context of the call that generated the condition whereas the handlers intryCatch()
are called in the context oftryCatch()
.