try_catch: Try to evaluate an expressions and capture any messages,...

Description Usage Arguments Details Value Examples

View source: R/try-catch.R

Description

try_catch() is similar to tryCatch(), except that, by default, errors are captured and presented using error(). Messages and warnings are not captured by this function. In addition, a "finally" expression can be specified which is evaluated at the end of the call no matter the result.

Usage

1
2
3
try_catch(expr, on_error, finally = NULL)

try_silently(expr)

Arguments

expr

(expression) The expression to evaluate

on_error

(function, optional) A function describing what to do in the event of a error in the above expression. The function must take a single argument, which is the simpleError(). If missing or NULL, errors are not caught. Default: error() called with the error's message prefixed by the calling function name.

finally

(expression, optional) An expression to evaluate at the end of the call. If missing or NULL, nothing is actioned.

Details

try_silenty() is similar to try(..., silently = TRUE) however if there is an error it returns NULL. All messages and warnings are also suppressed.

Value

The result of the evaluated expression

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
## Not run: 

try_catch(x <- "foo")
try_catch(stop("This is an error"))

try_silently("This is fine")
try_silently(stop("This is an error"))


## End(Not run)

ChadGoymer/msgr documentation built on April 10, 2021, 10:31 a.m.