看来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")
}
使用与
withCallingHandlers
相同的片段不打印“之后”并停止执行:f2 <- function() {
cat("before tryCatch\n")
withCallingHandlers({
stop("this is an error!")
},
error = function(cond) {
print(cond$message)
}
)
cat("after tryCatch\n")
}
我究竟做错了什么?
一些上下文
我想使用
withCallingHandlers
使用 sys.calls()
分析发生错误时的调用堆栈.根据Advanced R这应该是可能的:
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()
.
最佳答案
调用处理程序提供了一种在通过过程中“触摸”条件的方式,可能会在交互 session 中向用户发出信号之前将错误记录到文件中。
如果调用处理程序实际上没有返回,则调用处理程序可用于“消除”警告、消息或错误。您可以使用重新启动使调用处理程序不返回 -- 将您希望在调用 withRestarts()
时继续执行的代码括起来。 ,并在处理程序中调用重新启动:
f2 <- function() {
cat("before tryCatch\n")
withCallingHandlers({
withRestarts({
stop("this is an error!")
}, muffleStop=function() {
message("'stop' muffled")
})
},
error = function(cond) {
print(cond$message)
invokeRestart("muffleStop")
}
)
cat("after tryCatch\n")
}
更常见的是,重新启动是在一段代码中建立的(如在内置函数
warning
中)并在完全独立的代码块中调用(如内置函数 suppressWarnings
:> warning
function (..., call. = TRUE, immediate. = FALSE, noBreaks. = FALSE,
domain = NULL)
{
##
## ...
##
withRestarts({
.Internal(.signalCondition(cond, message, call))
.Internal(.dfltWarn(message, call))
}, muffleWarning = function() NULL)
##
## ...
##
}
<bytecode: 0x51a4730>
<environment: namespace:base>
> suppressWarnings
function (expr)
{
ops <- options(warn = -1)
on.exit(options(ops))
withCallingHandlers(expr,
warning = function(w) invokeRestart("muffleWarning"))
}
<bytecode: 0x35c2a60>
<environment: namespace:base>
关于r - 为什么 withCallingHandlers 仍然停止执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32167959/