##log(-1)
try(log("not a number"),silent=TRUE)
print("errors can't stop me")
##Use tryCatch to handle errors
an.error.occured <- FALSE
tryCatch( { result <- log("not a number"); print(res) }
, error = function(e) {an.error.occured <<- TRUE})
print(an.error.occured)
##tryCatch can handle all conditions 1/2
tryCatch( { result <- log(-1); print(result) }
, warning = function(w) { print("Hey, a warning") })
##tryCatch can handle all conditions 2/2
last.message <- NULL
tryCatch( { message("please handle me"); print("Done") }
, message = function(m) { last.message <<- m })
print(last.message$message)
##Advanced topic: User defined conditions
condition <- function(subclass, message, call = sys.call(-1), ...) {
structure( class = c(subclass, "condition"),
list(message = message, call = call, ...))
}
tryCatch( { work <- condition("my.work.condition", "after work party")
signalCondition(work); print("Done") }
, my.work.condition = function(c) { print(c$message) })
To embed this program on your website, copy the following code and paste it into your website's HTML: