Thursday, 15 August 2013

tryCatch:Error not caught

tryCatch:Error not caught

Please excuse if the question sounds naive. While trying to
closeAllConnections() in a tryCatch() block after trying to open file
connections recursively, it seems like the error is not caught properly.
Here is the sample code:
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()})
The above code throws: Error in file(aFile, "a") : all connections are in
use and doesn't close the connections. Is it the expected behaviour? (I
doubt that, please correct me if I'm missing something here)
PS: to close the connections I have few work arounds like adding a finally
and close them there.

No comments:

Post a Comment