错误时使用 tryCatch 跳到循环的下一个值? [英] Use tryCatch skip to next value of loop upon error?

查看:53
本文介绍了错误时使用 tryCatch 跳到循环的下一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些其他关于 tryCatch 和 cuzzins 的 SO 问题,以及文档:

I've read a few other SO questions about tryCatch and cuzzins, as well as the documentation:

但我还是不明白.

我正在运行一个循环,如果出现几种错误中的任何一种,我想跳到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

(顺便说一下,我找不到关于 next 的文档)

当我运行这个时,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 的关键是意识到它返回一个对象.如果 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 的含义是什么?这让我感到困惑,我认为文档中没有很好地解释.发生的情况是该参数捕获源自您正在tryCatching 的表达式中的任何错误消息.如果捕获到错误,则将其作为 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 tryCatching. 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` 中.


The function next is documented inside ?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屋!

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