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

标签 r error-handling

看来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/

相关文章:

r - 如何更改数据框中的列名称(包括点)

r - SparkR 与 sparklyr

html - 使用 HTML 或 CSS 居中对齐 Shiny box header

R - model.frame() 和非标准评估

python - 处理捕获的异常并将其引发到 try block 之外的正确方法是什么

google-apps-script - 愚蠢的脚本错误

r - 卡在 Twitter 授权的 "Redirecting you back to the application. This may take a few moments."部分 - 使用 twitteR 包

swift - 了解 CoreData 的错误处理

java - onError 未在 RxJava2 中实现错误,即使它已实现

php - 提供被调用者对 “call to undefined method”错误的响应?