tryCatch is indeed one such way to handle errors in a loop.
purrr is another way to do the same:
library(magrittr)
safe_log <- purrr::possibly(log, otherwise = 0)
list(2, 10, "34", 15) %>%
purrr::map(safe_log)
#> [[1]]
#> [1] 0.6931472
#>
#> [[2]]
#> [1] 2.302585
#>
#> [[3]]
#> [1] 0
#>
#> [[4]]
#> [1] 2.70805
Created on 2018-12-28 by the reprex package (v0.2.1)
As you can see, "34" would have thrown an error and you wouldn't get any result back. But since log is wrapped into purrr::possibly, it doesn't throw an error anymore.