Tsallaka zuwa abun ciki
Justice Tech School

Errors and tryCatch

tryCatch handles errors and warnings. stop and warning raise them.

Example
res <- tryCatch({
  stop("boom")
}, error = function(e) {
  paste("caught:", conditionMessage(e))
}, finally = {
  cat("done\n")
})
print(res)
print(tryCatch(as.integer("x"), warning = function(w) -1L))

Exercise

Raise an error.

("message")

Quiz Test

What does finally do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.