gpt4 book ai didi

r - 为什么 withCallingHandlers 仍然停止执行?

转载 作者:行者123 更新时间:2023-12-04 09:08:34 24 4
gpt4 key购买 nike

看来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 in tryCatch() are called in the context of tryCatch().

最佳答案

调用处理程序提供了一种在通过过程中“触摸”条件的方式,可能会在交互 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com