tryCatch:未捕获错误
如果这个问题听起来很幼稚,请原谅。 在尝试递归打开文件连接后,在tryCatch:未捕获错误,r,error-handling,try-catch,R,Error Handling,Try Catch,如果这个问题听起来很幼稚,请原谅。 在尝试递归打开文件连接后,在tryCatch()块中尝试closeAllConnections()时,似乎未正确捕获错误 以下是示例代码: fileOpenRec<-function(iter){ if(iter<130){ try( { aFile="file1.txt" fileCon<-file(aFile, "a") fileOpenRec
tryCatch()
块中尝试closeAllConnections()
时,似乎未正确捕获错误
以下是示例代码:
fileOpenRec<-function(iter){
if(iter<130){
try(
{
aFile="file1.txt"
fileCon<-file(aFile, "a")
fileOpenRec(iter+1)
}
)
}
}
tryCatch(fileOpenRec(1), error=function(e){print("Error!");closeAllConnections()})
fileOpenRec比较
> tryCatch({ stop("oops"); 1 }, error=function(err) "caught")
[1] "caught"
与
内部try()
捕捉(并打印)错误,因此外部tryCatch
与此无关。从代码中删除try()
。感谢Martin指出了其中的愚蠢之处。我应该看到的。
> tryCatch({ try(stop("oops")); 1 }, error=function(err) "caught")
Error in try(stop("oops")) : oops
[1] 1