r - tryCatch:Error not caught -


please excuse if question sounds naive. while trying closeallconnections() in trycatch() block after trying open file connections recursively, seems error not caught properly.

here 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") : connections in use , doesn't close connections. expected behaviour? (i doubt that, please correct me if i'm missing here)

ps: close connections have few work arounds adding finally , close them there.

compare

> trycatch({ stop("oops"); 1 }, error=function(err) "caught") [1] "caught" 

with

> trycatch({ try(stop("oops")); 1 }, error=function(err) "caught") error in try(stop("oops")) : oops [1] 1 

the inner try() catches (and prints) error, outer trycatch has nothing do. remove try() code.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -