next element in a loop if error in r

for (i in 1:10) {
  result <- tryCatch({
    # Code that might throw an error
    if (i == 5) {
      stop("Error occurred at i=5")
    }
    print(paste("Iteration", i))
  }, error = function(e) {
    # Error handling
    print(paste("Error occurred:", e$message))
    return(NULL)
  })

  if (is.null(result)) {
    next
  }

  # Continue with the rest of the loop if no error
  print("Continuing with the loop...")
}