为什么CallingHandlers仍然停止执行? [英] Why does withCallingHandlers still stops execution?

查看:142
本文介绍了为什么CallingHandlers仍然停止执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎 withCallingHandlers 实际上并没有以 tryCatch 的方式捕获错误,脚本仍然停止执行

It seems that withCallingHandlers doesn't actually catch the error the way that tryCatch does and the script still stops executing.

将代码段与 tryCatch 进行比较,其中打印before和after两者:

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与 withCallingHandlers 相同的代码段,不会打印after并停止执行:

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")
}

我做错了什么?

某些上下文

我想使用 withCallingHandlers 在发生错误的时候使用 sys.calls()

I'd like to use withCallingHandlers to analyze the call stack at the point where the error occurs using sys.calls().

根据 Advanced R 应该是可能的:


处理程序在 withCallingHandlers()在调用生成条件的上下文中调用,而 tryCatch()中的处理程序是在 tryCatch()的上下文中调用。

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().


推荐答案

调用处理程序提供了一种触摸一个条件的方法,也许在向用户发送交互式会话之前将错误记录到文件中。

Calling handlers provide a way of 'touching' a condition on the way through, maybe logging an error to a file before signalling the user in an interactive session.

调用处理程序可用于消除警告,消息或错误调用处理程序实际上没有返回。您可以使用重新启动使呼叫处理程序不返回 - 围绕您希望继续执行的代码,在调用$ code> withRestarts()中,并调用重新启动处理程序:

Calling handlers can be used to 'muffle' warnings, messages, or errors if the calling handler doesn't actually return. You can make a calling handler not return using restarts -- surround the code that you'd like to continue executing in a call to withRestarts(), and invoke the restart in the handler:

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

It's more usual that the restarts are established in one chunk of code (like in the built-in function warning) and invoked in a completely independent chunk of code (like the built-in function 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>

这篇关于为什么CallingHandlers仍然停止执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆