Skip to content
Prev 378735 / 398502 Next

Error trapping in R

Hello,

You can trap errors with ?try or ?tryCatch.
Example:


result <- vector(mode = "list", length = 5)
for(i in 1:5){
   result[[i]] <- tryCatch(if(i == 3) stop("This is an error") else 2*i + 1,
            error = function(e) e)
}

result

for(i in seq_along(result)) {
   err <- inherits(result[[i]], "error")
   print(err)
}


Hope this helps,

Rui Barradas

?s 20:55 de 27/02/2019, Bernard Comcast escreveu: