How to catch exception in R not to stop execution of program?

I'm trying to catch a error in the execution of code in the R language,I use "trycatch" in the cycle. However, when an error caught, the execution stops. How to make sure that the error has caught and the execution of cycle will continue?

Thank you!

Upvotes: 1

Views: 1113

Answers (1)

Rentrop
Rentrop

Reputation: 21507

You can use try and tryCatch to do this. Example:

for(i in 1:3){
  try(stop(sprintf("error no %d", i)))
}

gives you

Error in try(stop(sprintf("error no %d", i))) : error no 1
Error in try(stop(sprintf("error no %d", i))) : error no 2
Error in try(stop(sprintf("error no %d", i))) : error no 3

So execution is not stopped at any point

Upvotes: 1

Related Questions