Skip to content

Instantly share code, notes, and snippets.

@jilmun
Last active March 1, 2016 11:45
Show Gist options
  • Save jilmun/fa8438cecbcb32254388 to your computer and use it in GitHub Desktop.
Save jilmun/fa8438cecbcb32254388 to your computer and use it in GitHub Desktop.
# custom tryCatch to return result and warnings -- http://stackoverflow.com/a/24569739/2271856
myTryCatch <- function(expr) {
warn <- err <- NULL
value <- withCallingHandlers(
tryCatch(expr, error=function(e) {
err <<- e
NULL
}), warning=function(w) {
warn <<- w
invokeRestart("muffleWarning")
})
list(value=value, warning=warn, error=err)
}
# example use cases
myTryCatch(1+1) # legit
myTryCatch(log(-1)) # NaN + warning
myTryCatch("a"+1) # error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment