- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
看来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()
分析发生错误时的调用堆栈.
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/
看来withCallingHandlers实际上并没有像 tryCatch 那样捕获错误。确实如此,脚本仍然停止执行。 将片段与 tryCatch 进行比较打印“之前”和“之后”的位置: f1 wa
问题 我正在尝试编写一个函数,该函数将评估代码并存储结果,包括代码中表示的任何可能的条件。除了在错误处理表达式中运行我的函数(我们称其为evalcapt)的情况之外,我的工作非常正常。 问题是with
背景 我有一个简单的脚本,它遍历目录中可用的 CSV 文件,并使用 read_csv 将它们读入一个数据框。和 map_dfr分别发挥作用。该脚本可能会遇到缺少列的文件。在那种情况下 read_csv
我需要将警告转换为错误,以便能够在上游进一步处理警告(警告被吞没在我无法控制的中间位置;错误没有出现)。为此,我正在执行以下操作: warning_to_error = function (expr)
我一直在使用基于 plyr 的函数 summarySE和 ddply几个月没有任何问题。今天,当我在 R 中运行我极其基本的例程时,出现了一些错误消息并导致 R 崩溃。 这是一个示例代码和我在 R 崩
我正在尝试编写一个文档,讨论使用错误向用户传达参数问题。不幸的是,我似乎无法获得 .Rmd 文件来编织。一个简短的例子: Intro text ```{r} some_function ... wi
我是一名优秀的程序员,十分优秀!