Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

r - Why does withCallingHandlers still stops execution?

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

Compare the snippet with tryCatch where both "before" and "after" are printed:

f1 <- function() {
  cat("before tryCatch
")
  tryCatch({
      stop("this is an error!")
    },
    error = function(cond) {
      print(cond$message)
    }
  )
  cat("after tryCatch
")
}

With the same snippet with withCallingHandlers that doesn't print "after" and stops execution:

f2 <- function() {
  cat("before tryCatch
")
  withCallingHandlers({
      stop("this is an error!")
    },
    error = function(cond) {
      print(cond$message)
    }
  )
  cat("after tryCatch
")
}

What am I doing wrong?

Some context

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

According to Advanced R that should be possible:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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
")
  withCallingHandlers({
      withRestarts({
          stop("this is an error!")
      }, muffleStop=function() {
          message("'stop' muffled")
      })
    },
    error = function(cond) {
      print(cond$message)
      invokeRestart("muffleStop")
    }
  )
  cat("after tryCatch
")
}

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...