英文:
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 <- 0 before the for loop, there are a couple of options:
My preferred is to catch and check to see if it inherits "error":
errors <- 0L
for (...) {
  res <- tryCatch({
    ...API call...
  },
    error = function(e) e)
  if (inherits(res, "error")) {
    errors <- errors + 1L
  } else {
    something else here
  }
}
Equivalently with try:
errors <- 0L
for (...) {
  res <- try({
    ...API call...
  }, silent = TRUE)
  if (inherits(res, "try-error")) {
    errors <- errors + 1L
  } else {
    something else here
  }
}
Though the reasons I prefer that may be mostly subjective. Another way would be more internal:
errors <- 0L
for (...) {
  res <- tryCatch({
    ...API call...
  },
    error = function(e) { errors <<- 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 <- function(in_1){
  if(in_1 %in% c(5,7)){
    stop("bad num")
  }
  in_1+.5
}
for(i in 1:10){
  print(api_call(i))
}
# wrap it up
library(purrr)
safe_api_call <- safely(api_call)
for(i in 1:10){
  print(safe_api_call(i)$result)
}
# add counting of errors
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
</details>
				通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论