似乎withCallingHandlers实际上并没有捕获错误tryCatch,脚本仍然停止执行.
将代码段与tryCatch打印"之前"和"之后"的位置进行比较:
f1 <- function() {
cat("before tryCatch\n")
tryCatch({
stop("this is an error!")
},
error = function(cond) {
print(cond$message)
}
)
cat("after tryCatch\n")
}
Run Code Online (Sandbox Code Playgroud)
使用相同的片段withCallingHandlers不会打印"之后"并停止执行:
f2 <- function() {
cat("before tryCatch\n")
withCallingHandlers({
stop("this is an error!")
},
error = function(cond) {
print(cond$message)
}
)
cat("after tryCatch\n")
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
一些背景
我想用它withCallingHandlers来分析发生错误时的调用堆栈sys.calls().
据Advanced R说,应该是可能的:
在处理程序
withCallingHandlers()被称为在产生而在处理条件下的呼叫的上下文中tryCatch()被称为在的上下文中tryCatch().