r tryCatch如何将对象传递给error函数 myFunc
参数r tryCatch如何将对象传递给error函数 myFunc,r,try-catch,R,Try Catch,参数error是一个处理程序,有文档记录(请参见?tryCatch)以接受一个参数(错误条件)。错误处理程序可以访问调用stop时可用的任何变量。所以 myFunc <- function(x) { x <- timeSeries(x, charvec=as.Date(index(x))) t<-tryCatch( doSomething(x), error=function(x) rep(0,ncol(x)) ) t } 捕获代码引发的错误,发现值i,并发
error
是一个处理程序,有文档记录(请参见?tryCatch
)以接受一个参数(错误条件)。错误处理程序可以访问调用stop
时可用的任何变量。所以
myFunc <- function(x)
{
x <- timeSeries(x, charvec=as.Date(index(x)))
t<-tryCatch( doSomething(x), error=function(x) rep(0,ncol(x))
)
t
}
捕获代码引发的错误,发现值i
,并发出信息更丰富的消息。所以我猜
f = function() {
tryCatch({
i = 1
stop("oops")
}, error=function(e) {
stop(conditionMessage(e), " when 'i' was ", i)
})
}
myFunc读取错误消息:它的意思是ncol(x)
是NA(或者可能是NULL或不太可能是Inf)。您没有说timeSeries属于哪个包,但我猜它不会返回包含列的对象。
myFunc <- function(x)
{
tryCatch({
x <- timeSeries(x, charvec=as.Date(index(x)))
doSomething(x)
}, error=function(...) rep(0, ncol(x)))
}