Use tryCatch skip to next value of loop upon error?
The key to using tryCatch
is realising that it returns an object. If there was an error inside the tryCatch
then this object will inherit from class error
. You can test for class inheritance with the function inherit
.
x <- tryCatch(stop("Error"), error = function(e) e)class(x)"simpleError" "error" "condition"
Edit:
What is the meaning of the argument error = function(e) e
? This baffled me, and I don't think it's well explained in the documentation. What happens is that this argument catches any error messages that originate in the expression that you are tryCatch
ing. If an error is caught, it gets returned as the value of tryCatch
. In the help documentation this is described as a calling handler
. The argument e
inside error=function(e)
is the error message originating in your code.
I come from the old school of procedural programming where using next
was a bad thing. So I would rewrite your code something like this. (Note that I removed the next
statement inside the 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
The function next
is documented inside ?
for`.
If you want to use that instead of having your main working routine inside an if
, your code should look something like this:
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
I found other answers very confusing. Here is an extremely simple implementation for anyone who wants to simply skip to the next loop iteration in the event of an error
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 } }
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 = "")) }