[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  try: Try an Expression Allowing Error Recovery

##  Description

` try ` is a wrapper to run an expression that might fail and allow the user's
code to handle error-recovery.

##  Usage

    
    
    try(expr, silent = FALSE,
        outFile = getOption("try.outFile", default = stderr()))

##  Arguments

expr

an  R  expression to try.

silent

logical: should the report of error messages be suppressed?

outFile

a [ connection ](/link/connection?package=base&version=3.6.2) , or a character
string naming the file to print to (via ` [ cat
](/link/cat?package=base&version=3.6.2) (*, file = outFile) ` ); used only if
` silent ` is false, as by default.

##  Value

The value of the expression if ` expr ` is evaluated without error, but an
invisible object of class ` "try-error" ` containing the error message, and
the error condition as the ` "condition" ` attribute, if it fails.

##  Details

` try ` evaluates an expression and traps any errors that occur during the
evaluation. If an error occurs then the error message is printed to the ` [
stderr ](/link/stderr?package=base&version=3.6.2) ` connection unless `
options("show.error.messages") ` is false or the call includes ` silent = TRUE
` . The error message is also stored in a buffer where it can be retrieved by
` geterrmessage ` . (This should not be needed as the value returned in case
of an error contains the error message.)

` try ` is implemented using ` [ tryCatch
](/link/tryCatch?package=base&version=3.6.2) ` ; for programming, instead of `
try(expr, silent = TRUE) ` , something like ` tryCatch(expr, error =
function(e) e) ` (or other simple error handler functions) may be more
efficient and flexible.

It may be useful to set the default for ` outFile ` to ` [ stdout
](/link/stdout?package=base&version=3.6.2) () ` , i.e.,

    
    
      options(try.outFile = stdout()) 

instead of the default ` [ stderr ](/link/stderr?package=base&version=3.6.2)
() ` , notably when ` try() ` is used inside a ` [ Sweave
](/link/Sweave?package=base&version=3.6.2) ` code chunk and the error message
should appear in the resulting document.

##  See Also

` [ options ](/link/options?package=base&version=3.6.2) ` for setting error
handlers and suppressing the printing of error messages; ` [ geterrmessage
](/link/geterrmessage?package=base&version=3.6.2) ` for retrieving the last
error message. The underlying ` [ tryCatch
](/link/tryCatch?package=base&version=3.6.2) ` provides more flexible means of
catching and handling errors.

` [ assertCondition ](/link/assertCondition?package=base&version=3.6.2) ` in
package  tools  is related and useful for testing.

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Ftry&utm_source=r-docs&utm_medium=docs&utm_term=try&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    ## this example will not work correctly in example(try), but
    ## it does work correctly if pasted in
    options(show.error.messages = FALSE)
    try(log("a"))
    print(.Last.value)
    options(show.error.messages = TRUE)
    
    ## alternatively,
    print(try(log("a"), TRUE))
    
    ## run a simulation, keep only the results that worked.
    set.seed(123)
    x <- stats::rnorm(50)
    doit <- function(x)
    {
        x <- sample(x, replace = TRUE)
        if(length(unique(x)) > 30) mean(x)
        else stop("too few unique points")
    }
    ## alternative 1
    res <- lapply(1:100, function(i) try(doit(x), TRUE))
    ## alternative 2
    # }
    # NOT RUN {
    res <- vector("list", 100)
    for(i in 1:100) res[[i]] <- try(doit(x), TRUE)
    # }
    # NOT RUN {
    unlist(res[sapply(res, function(x) !inherits(x, "try-error"))])
    # }
    

Run the