使用tryCatch会在出错时跳到循环的下一个值吗? [英] Use tryCatch skip to next value of loop upon error?
问题描述
我还阅读了有关 tryCatch
和cuzzins以及文档的其他一些问题:
I've read a few other SO questions about tryCatch
and cuzzins, as well as the documentation:
- Exception handling in R
- catching an error and then branching logic
- How can I check whether a function call results in a warning?
- Problems with Plots in Loop
但是我还是不明白。
我正在循环运行,想跳到 next
如果发生以下几种错误中的任何一种:
I'm running a loop and want to skip to next
if any of a few kinds of errors occur:
for (i in 1:39487) {
# EXCEPTION HANDLING
this.could.go.wrong <- tryCatch(
attemptsomething(),
error=function(e) next
)
so.could.this <- tryCatch(
doesthisfail(),
error=function(e) next
)
catch.all.errors <- function() { this.could.go.wrong; so.could.this; }
catch.all.errors;
#REAL WORK
useful(i); fun(i); good(i);
} #end for
(顺便说一句,是我找不到的下一步
的文档)
(by the way, there is no documentation for next
that I can find)
运行此命令时, R
鸣喇叭:
When I run this, R
honks:
Error in value[[3L]](cond) : no loop for break/next, jumping to top level
我在这里缺少什么基本要点? tryCatch
显然在 for
循环中,所以为什么不 R
知道吗?
What basic point am I missing here? The tryCatch
's are clearly within the for
loop, so why doesn't R
know that?
推荐答案
使用 tryCatch $ c $的关键c>正在意识到它返回一个对象。如果
tryCatch
内部发生错误,则此对象将从类 error
继承。您可以使用函数 inherit
来测试类的继承。
The key to using tryCatch
is realising that it returns an object. If there was an error inside the tryCatch
then this object will inherit from class error
. You can test for class inheritance with the function inherit
.
x <- tryCatch(stop("Error"), error = function(e) e)
class(x)
"simpleError" "error" "condition"
编辑:
参数的含义是什么 error = function(e)e
?这让我感到困惑,我认为文档中对此的解释不充分。发生的是,此参数捕获了您正在 tryCatch
的表达式中产生的所有错误消息。如果捕获到错误,它将作为 tryCatch
的值返回。在帮助文档中,这被称为调用处理程序
。 error = function(e)
中的参数 e
是源于您代码的错误消息。
What is the meaning of the argument error = function(e) e
? This baffled me, and I don't think it's well explained in the documentation. What happens is that this argument catches any error messages that originate in the expression that you are tryCatch
ing. If an error is caught, it gets returned as the value of tryCatch
. In the help documentation this is described as a calling handler
. The argument e
inside error=function(e)
is the error message originating in your code.
我来自过程编程的老派,在那儿使用 next
是一件坏事。所以我会像这样重写您的代码。 (请注意,我删除了 tryCatch
内的 next
语句。)
I come from the old school of procedural programming where using next
was a bad thing. So I would rewrite your code something like this. (Note that I removed the next
statement inside the tryCatch
.):
for (i in 1:39487) {
#ERROR HANDLING
possibleError <- tryCatch(
thing(),
error=function(e) e
)
if(!inherits(possibleError, "error")){
#REAL WORK
useful(i); fun(i); good(i);
}
} #end for
函数 next
记录在?
for中。
如果要使用它而不是将主工作例程放在 if
中,则代码应如下所示:
If you want to use that instead of having your main working routine inside an if
, your code should look something like this:
for (i in 1:39487) {
#ERROR HANDLING
possibleError <- tryCatch(
thing(),
error=function(e) e
)
if(inherits(possibleError, "error")) next
#REAL WORK
useful(i); fun(i); good(i);
} #end for
这篇关于使用tryCatch会在出错时跳到循环的下一个值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!