【发布时间】:2014-01-03 00:23:28
【问题描述】:
问题
似乎在函数中,当您计算一个多次产生错误的表达式时,您会收到警告restarting interrupted promise evaluation。例如:
foo <- function() stop("Foo error")
bar <- function(x) {
try(x)
x
}
bar(foo())
产量
Error in foo() : Foo error
Error in foo() : Foo error
In addition: Warning message:
In bar(foo()) : restarting interrupted promise evaluation
如何避免这个警告并妥善处理?
背景
尤其是像写入数据库这样的操作,您可能会遇到需要您重试操作几次的锁定错误。因此,我创建了一个围绕 tryCatch 的包装器,它会重新评估一个表达式,直到成功为止 n 次:
tryAgain <- function(expr, n = 3) {
success <- T
for (i in 1:n) {
res <- tryCatch(expr,
error = function(e) {
print(sprintf("Log error to file: %s", conditionMessage(e)))
success <<- F
e
}
)
if (success) break
}
res
}
但是,我收到了大量 restarting interrupted promise evaluation 消息:
> tryAgain(foo())
[1] "Log error to file: Foo error"
[1] "Log error to file: Foo error"
[1] "Log error to file: Foo error"
<simpleError in foo(): Foo error>
Warning messages:
1: In doTryCatch(return(expr), name, parentenv, handler) :
restarting interrupted promise evaluation
2: In doTryCatch(return(expr), name, parentenv, handler) :
restarting interrupted promise evaluation
理想情况下,我希望完全避免这些消息,而不是仅仅将它们屏蔽掉,因为我可能还想处理来自expr 的真正警告。
【问题讨论】:
标签: r error-handling try-catch