using try vs TryCatch in R

huangapple go评论82阅读模式
英文:

using try vs TryCatch in R

问题

我的R代码如下:

errors = 0

for(i in c(1:100)){

 tryCatch(expr = {
    API调用
  }, error = {errors=errors+1}, finally = {})

  后续代码
}

这段代码的目的是即使API调用失败,也要继续执行,并计算出错误发生的次数。
但是我发现,如果在某次迭代中API调用出现错误,那么后续的迭代代码将不会执行。
如果我使用try(),那么我将无法计算错误的次数。

英文:

My R code is as follows

errors = 0

for(i in c(1:100)){

 tryCatch(expr = {
    API Call
  }, error = {errors=errors+1}, finally = {})

  further code
}

The code is meant to continue execution even if the API call fails and count the number of times error occurred.
But I see that if for an iteration there is an error in API call, the code is not executed for further iterations.
If I use try(), then I wont be able to count the number of errors.

答案1

得分: 3

tryCatch函数的error=参数应该是一个函数。如果在for循环之前预先实例化errors <- 0,有几种选择:

我更喜欢的是捕获并检查是否继承了"error"

errors <- 0L
for (...) {
  res <- tryCatch({
    ...API调用...
  },
    error = function(e) e)
  if (inherits(res, "error")) {
    errors <- errors + 1L
  } else {
    这里是其他操作
  }
}

等效的方式是使用try

errors <- 0L
for (...) {
  res <- try({
    ...API调用...
  }, silent = TRUE)
  if (inherits(res, "try-error")) {
    errors <- errors + 1L
  } else {
    这里是其他操作
  }
}

虽然我更喜欢这种方式可能主要是主观的原因。另一种方式是更内部的:

errors <- 0L
for (...) {
  res <- tryCatch({
    ...API调用...
  },
    error = function(e) { errors <<- errors + 1L; })
  这里是其他操作
}
英文:

The error= argument of tryCatch should be a function. If you pre-instantiate errors &lt;- 0 before the for loop, there are a couple of options:

My preferred is to catch and check to see if it inherits &quot;error&quot;:

errors &lt;- 0L
for (...) {
  res &lt;- tryCatch({
    ...API call...
  },
    error = function(e) e)
  if (inherits(res, &quot;error&quot;)) {
    errors &lt;- errors + 1L
  } else {
    something else here
  }
}

Equivalently with try:

errors &lt;- 0L
for (...) {
  res &lt;- try({
    ...API call...
  }, silent = TRUE)
  if (inherits(res, &quot;try-error&quot;)) {
    errors &lt;- errors + 1L
  } else {
    something else here
  }
}

Though the reasons I prefer that may be mostly subjective. Another way would be more internal:

errors &lt;- 0L
for (...) {
  res &lt;- tryCatch({
    ...API call...
  },
    error = function(e) { errors &lt;&lt;- errors + 1L; })
  something else here
}

答案2

得分: 0


# 设置场景
api_call <- function(in_1){
  if(in_1 %in% c(5,7)){
    stop("bad num")
  }
  in_1 + 0.5
}

for(i in 1:10){
  print(api_call(i))
}

# 封装函数
library(purrr)
safe_api_call <- safely(api_call)
for(i in 1:10){
  print(safe_api_call(i)$result)
}

# 添加错误计数
error_count <- 0
for(i in 1:10){
  r <- safe_api_call(i)
  print(r$result)
  error_count <- error_count + inherits(r$error, "error")
}
error_count

以上是要翻译的内容。

英文:

# set up scenario
api_call &lt;- function(in_1){
  if(in_1 %in% c(5,7)){
    stop(&quot;bad num&quot;)
  }
  in_1+.5
}

for(i in 1:10){
  print(api_call(i))
}

# wrap it up
library(purrr)
safe_api_call &lt;- safely(api_call)
for(i in 1:10){
  print(safe_api_call(i)$result)
}

# add counting of errors

error_count &lt;- 0
for(i in 1:10){
  r &lt;- safe_api_call(i)
  print(r$result)
  error_count &lt;- error_count + inherits(r$error,&quot;error&quot;)
}
error_count

</details>



huangapple
  • 本文由 发表于 2023年8月9日 03:13:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定