Gar*_*ini 4 exception-handling r variable-assignment rcurl
while(bo!=10){
x = tryCatch(getURLContent(Site, verbose = F, curl = handle),
error = function(e) {
cat("ERROR1: ", e$message, "\n")
Sys.sleep(1)
print("reconntecting...")
bo <- bo+1
print(bo)
})
print(bo)
if(bo==0) bo=10
}
Run Code Online (Sandbox Code Playgroud)
我想尝试在连接失败后重新连接每秒.但是bo值的新赋值是无效的.我怎样才能做到这一点?或者,如果您知道如何使用RCurl选项重新连接(我真的没有找到任何东西),那将是惊人的.
感谢每一位帮助
问题是b0分配的范围。但是,我发现try比友好得多tryCatch。这应该工作:
while(bo!=10){
x = try(getURLContent(Site, verbose = F, curl = handle),silent=TRUE)
if (class(x)=="try-error") {
cat("ERROR1: ", x, "\n")
Sys.sleep(1)
print("reconnecting...")
bo <- bo+1
print(bo)
} else {
break
}
}
Run Code Online (Sandbox Code Playgroud)
以上尝试10次连接到该站点。如果任何一次成功,它将退出。
创建范围之外的变量tryCatch(),并使用更新<<-
bo <- 0
while(bo!=10){
x = tryCatch(stop("failed"),
error = function(e) {
bo <<- bo + 1L
message("bo: ", bo, " " conditionMessage(e))
})
}
Run Code Online (Sandbox Code Playgroud)
或者使用返回值作为成功的哨兵
x <- 1
while (is.numeric(x)) {
x = tryCatch({
stop("failed")
}, error = function(e) {
message("attempt: ", x, " ", conditionMessage(e))
if (x > 10) stop("too many failures", call.=FALSE)
x + 1
})
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
494 次 |
| 最近记录: |