iso*_*mes 57 error-handling exception-handling r try-catch
我已经阅读了一些关于tryCatch和cuzzins的其他SO问题,以及文档:
但我还是不明白.
我正在运行一个循环,next如果发生任何一种错误,我想跳过:
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
Run Code Online (Sandbox Code Playgroud)
(顺便说一句,没有next我能找到的文件)
当我跑步时,R鸣喇叭:
Error in value[[3L]](cond) : no loop for break/next, jumping to top level
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么基本点?这tryCatch显然是在for循环中,所以为什么不R知道呢?
And*_*rie 80
使用的关键tryCatch是意识到它返回一个对象.如果内部存在错误,tryCatch则此对象将从类继承error.您可以使用该函数测试类继承inherit.
x <- tryCatch(stop("Error"), error = function(e) e)
class(x)
"simpleError" "error" "condition"
Run Code Online (Sandbox Code Playgroud)
编辑:
争论的意义是error = function(e) e什么?这让我感到困惑,我认为在文档中没有很好地解释它.会发生的是,此参数捕获源自您正在使用的表达式的任何错误消息tryCatch.如果捕获到错误,则将其作为值返回tryCatch.在帮助文档中,这被描述为a calling handler.e里面的参数error=function(e)是源自代码的错误消息.
我来自程序式编程的旧学校,在那里使用next是一件坏事.所以我会像这样重写你的代码.(注意我删除了next里面的语句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
Run Code Online (Sandbox Code Playgroud)
该函数next记录在?` 里面.
如果你想使用它而不是让你的主要工作例程if,你的代码应该是这样的:
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
Run Code Online (Sandbox Code Playgroud)
use*_*745 51
我发现其他答案非常令人困惑。这是一个非常简单的实现,适用于任何想要在发生错误时简单地跳到下一个循环迭代的人
for (i in 1:10) {
skip_to_next <- FALSE
# Note that print(b) fails since b doesn't exist
tryCatch(print(b), error = function(e) { skip_to_next <<- TRUE})
if(skip_to_next) { next }
}
Run Code Online (Sandbox Code Playgroud)
我遗漏的一件事是,当在 R 中的 for 循环内运行函数时,它会跳出 for 循环,这一点很清楚:
next在函数内部不起作用。Voldemort = TRUE从函数内部(在我的例子中tryCatch)向外部发送一些信号或标志(例如,)。Voldemort == TRUE)。如果是这样,您可以在函数之外调用breakor 。next我看到的唯一真正详细的解释可以在这里找到:http : //mazamascience.com/WorkingWithData/?p=912
这是该博客文章中的代码片段,展示了 tryCatch 的工作原理
#!/usr/bin/env Rscript
# tryCatch.r -- experiments with tryCatch
# Get any arguments
arguments <- commandArgs(trailingOnly=TRUE)
a <- arguments[1]
# Define a division function that can issue warnings and errors
myDivide <- function(d, a) {
if (a == 'warning') {
return_value <- 'myDivide warning result'
warning("myDivide warning message")
} else if (a == 'error') {
return_value <- 'myDivide error result'
stop("myDivide error message")
} else {
return_value = d / as.numeric(a)
}
return(return_value)
}
# Evalute the desired series of expressions inside of tryCatch
result <- tryCatch({
b <- 2
c <- b^2
d <- c+2
if (a == 'suppress-warnings') {
e <- suppressWarnings(myDivide(d,a))
} else {
e <- myDivide(d,a) # 6/a
}
f <- e + 100
}, warning = function(war) {
# warning handler picks up where error was generated
print(paste("MY_WARNING: ",war))
b <- "changing 'b' inside the warning handler has no effect"
e <- myDivide(d,0.1) # =60
f <- e + 100
return(f)
}, error = function(err) {
# warning handler picks up where error was generated
print(paste("MY_ERROR: ",err))
b <- "changing 'b' inside the error handler has no effect"
e <- myDivide(d,0.01) # =600
f <- e + 100
return(f)
}, finally = {
print(paste("a =",a))
print(paste("b =",b))
print(paste("c =",c))
print(paste("d =",d))
# NOTE: Finally is evaluated in the context of of the inital
# NOTE: tryCatch block and 'e' will not exist if a warning
# NOTE: or error occurred.
#print(paste("e =",e))
}) # END tryCatch
print(paste("result =",result))
Run Code Online (Sandbox Code Playgroud)
小智 5
rm(list=ls())
for (i in -3:3) {
#ERROR HANDLING
possibleError <- tryCatch({
print(paste("Start Loop ", i ,sep=""))
if(i==0){
stop()
}
}
,
error=function(e) {
e
print(paste("Oops! --> Error in Loop ",i,sep = ""))
}
)
if(inherits(possibleError, "error")) next
print(paste(" End Loop ",i,sep = ""))
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
75464 次 |
| 最近记录: |