How to use the tryCatch() function?
Asked Answered
L

6

531

I want to write code using tryCatch to deal with errors downloading data from the web.

url <- c(
    "http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html",
    "http://en.wikipedia.org/wiki/Xz")
y <- mapply(readLines, con=url)

These two statements run successfully. Below, I create a non-exist web address:

url <- c("xxxxx", "http://en.wikipedia.org/wiki/Xz")

url[1] does not exist. How does one write a tryCatch loop (function) so that:

  1. When the URL is wrong, the output will be: "web URL is wrong, can't get".
  2. When the URL is wrong, the code does not stop, but continues to download until the end of the list of URLs?
Lineament answered 30/8, 2012 at 9:27 Comment(0)
A
922

Setting up the code

urls <- c(
    "http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html",
    "http://en.wikipedia.org/wiki/Xz",
    "xxxxx"
)

readUrl <- function(url) {
    tryCatch(
        {
            # Just to highlight: if you want to use more than one
            # R expression in the "try" part then you'll have to
            # use curly brackets.
            # 'tryCatch()' will return the last evaluated expression
            # in case the "try" part was completed successfully

            message("This is the 'try' part")

            suppressWarnings(readLines(url))
            # The return value of `readLines()` is the actual value
            # that will be returned in case there is no condition
            # (e.g. warning or error).
        },
        error = function(cond) {
            message(paste("URL does not seem to exist:", url))
            message("Here's the original error message:")
            message(conditionMessage(cond))
            # Choose a return value in case of error
            NA
        },
        warning = function(cond) {
            message(paste("URL caused a warning:", url))
            message("Here's the original warning message:")
            message(conditionMessage(cond))
            # Choose a return value in case of warning
            NULL
        },
        finally = {
            # NOTE:
            # Here goes everything that should be executed at the end,
            # regardless of success or error.
            # If you want more than one expression to be executed, then you
            # need to wrap them in curly brackets ({...}); otherwise you could
            # just have written 'finally = <expression>' 
            message(paste("Processed URL:", url))
            message("Some other message at the end")
        }
    )
}

Using the code

> y <- lapply(urls, readUrl)
This is the 'try' part
Processed URL: http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html
Some other message at the end
This is the 'try' part
Processed URL: http://en.wikipedia.org/wiki/Xz
Some other message at the end
This is the 'try' part
URL does not seem to exist: xxxxx
Here's the original error message:
cannot open the connection
Processed URL: xxxxx
Some other message at the end

Investigating the output

> head(y[[1]])
[1] "<!DOCTYPE html><html><head><title>R: Functions to Manipulate Connections (Files, URLs, ...)</title>"
[2] "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
[3] "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\" />"
[4] "<link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css\">"
[5] "<script type=\"text/javascript\">"
[6] "const macros = { \"\\\\R\": \"\\\\textsf{R}\", \"\\\\code\": \"\\\\texttt\"};"

> length(y)
[1] 3

> y[[3]]
[1] NA

Additional remarks

tryCatch

tryCatch returns the value associated to executing expr unless there's an error or a warning. In this case, specific return values (see NA above) can be specified by supplying a respective handler function (see arguments error and warning in ?tryCatch). These can be functions that already exist, but you can also define them within tryCatch() (as I did above).

The implications of choosing specific return values of the handler functions

As we've specified that NA should be returned in case of error, the third element in y is NA.

Ardenardency answered 30/8, 2012 at 9:27 Comment(16)
You should use paste0 for that!Appreciation
paste0() is in base. Internally, both paste() and paste0() call do_paste in paste.c. The only difference is paste0() does not pass a sep argument.Octachord
@Octachord + @seancarmody: sorry, true for R-2.15.1. I was running an outdated version of R (2.14.1) and back then there wasn't such a function. Very nice, IMHO that should have been the default behavior of paste right from the beginningArdenardency
is out will be NULL if a warning is issued in the tryCatch in your example?Piston
@RNA: actually, I think that's a little mistake in my code as there is no out defined in the scope of the warning function. Just by looking at it, I'm not sure what R will take as the value for out via lexical scoping. I'll checkArdenardency
@RNA: updated the answer. If you don't supply anything explicitly, the handler functions will return NULL. Returning out in the warning case didn't make any sense to me anyomre, so I changed it to return(NULL) just for illustrative purposes. Hope it's clear now.Ardenardency
@Rappster: yeah, thanks. and could you explain the last line return (out)too? Isn't this similar situation to the return in the warning function?Piston
I was wondering why my error bit was always being executed - curly braces fixed it!Shooin
How can I display a message if the try part was completed successfully please ?Preliminaries
@JulienNavarre: remember that the "try part" always returns the last object (currently readLines(con=url, warn=FALSE) which is the actual thing that could go wrong). So if you wanted to add a message, you would would need to stored the actual retun value in a variable: out <- readLines(con=url, warn=FALSE) followed by message("Everything worked") followed by out in order to make this the last object that is actually returnedArdenardency
How to preserve information on what went wrong? error = function(cond) { message(cond) } does not return the same amount of information that shows up without a trycatch. Anybody face this issue? How to print information of the line that caused the error.Ducal
Try conditionMessage (cond). That's the actual error message as a character string.Ardenardency
Fantastic example and well documented. May I request all commenters on frivolous questions like paste / paste0 to be deleted so that we do not crowd this section with irrelevant stuff? Thanks.Disenable
Is there a (nice) way to use the same functionality for both "warning" and "error" case? Or do I have to replicate the code?Tearoom
How to execute a command in finally only if there wasn't any error?Calcification
finally runs using on.exit() in tryCatch. This is handy for clean up tasks like closing connections, removing temporary files, restoring a state, etc. These are tasks you want to do regardless of if your code runs successfully. More information about this can be found here: How and when should I use on.exit?Madgemadhouse
V
184

tryCatch has a slightly complex syntax structure. However, once we understand the 4 parts which constitute a complete tryCatch call as shown below, it becomes easy to remember:

expr: [Required] R code(s) to be evaluated

error : [Optional] What should run if an error occured while evaluating the codes in expr

warning : [Optional] What should run if a warning occured while evaluating the codes in expr

finally : [Optional] What should run just before quitting the tryCatch call, irrespective of if expr ran successfully, with an error, or with a warning

tryCatch(
    expr = {
        # Your code...
        # goes here...
        # ...
    },
    error = function(e){ 
        # (Optional)
        # Do this if an error is caught...
    },
    warning = function(w){
        # (Optional)
        # Do this if a warning is caught...
    },
    finally = {
        # (Optional)
        # Do this at the end before quitting the tryCatch structure...
    }
)

Thus, a toy example, to calculate the log of a value might look like:

log_calculator <- function(x){
    tryCatch(
        expr = {
            message(log(x))
            message("Successfully executed the log(x) call.")
        },
        error = function(e){
            message('Caught an error!')
            print(e)
        },
        warning = function(w){
            message('Caught an warning!')
            print(w)
        },
        finally = {
            message('All done, quitting.')
        }
    )    
}

Now, running three cases:

A valid case

log_calculator(10)
# 2.30258509299405
# Successfully executed the log(x) call.
# All done, quitting.

A "warning" case

log_calculator(-10)
# Caught an warning!
# <simpleWarning in log(x): NaNs produced>
# All done, quitting.

An "error" case

log_calculator("log_me")
# Caught an error!
# <simpleError in log(x): non-numeric argument to mathematical function>
# All done, quitting.

I've written about some useful use-cases which I use regularly. Find more details here: Using tryCatch for robust R scripts

Hope this is helpful.

Vivl answered 20/12, 2018 at 21:56 Comment(3)
How to make log_calculator return the value?Damales
@Julien, the last line of expr gets returned if there is no error.Loathe
Is it also possible to have a return different than NULL when the expression is executed correctly?Terrorize
H
89

R uses functions for implementing try-catch block:

The syntax somewhat looks like this:

result = tryCatch({
    expr
}, warning = function(warning_condition) {
    warning-handler-code
}, error = function(error_condition) {
    error-handler-code
}, finally={
    cleanup-code
})

In tryCatch() there are two ‘conditions’ that can be handled: ‘warnings’ and ‘errors’. The important thing to understand when writing each block of code is the state of execution and the scope. @source

Hostetler answered 30/8, 2012 at 9:34 Comment(2)
Replace error-handler-code with cat("web url is wrong, can't get")Appreciation
you left out message-catchingHardpressed
S
69

Here goes a straightforward example:

# Do something, or tell me why it failed
my_update_function <- function(x){
    tryCatch(
        # This is what I want to do...
        {
        y = x * 2
        return(y)
        },
        # ... but if an error occurs, tell me what happened: 
        error=function(error_message) {
            message("This is my custom message.")
            message("And below is the error message from R:")
            message(error_message)
            return(NA)
        }
    )
}

If you also want to capture a "warning", just add warning= similar to the error= part.

Scutter answered 13/4, 2017 at 0:25 Comment(4)
Should there be curly brackets around the expr part, since there are two lines instead of one?Scutter
Thanks! After double checking, I don't see any need for curly bracketsScutter
Thanks for double checking. When I run your code, I got Error: unexpected ')' in " )" and Error: unexpected ')' in " )". Adding a pair of curly brackets solves the problem.Scutter
For most use cases, you are right, thank you! It has been fixed.Scutter
R
33

Since I just lost two days of my life trying to solve for tryCatch for an irr function, I thought I should share my wisdom (and what is missing). FYI - irr is an actual function from FinCal in this case where got errors in a few cases on a large data set.

  1. Set up tryCatch as part of a function. For example:

    irr2 <- function (x) {
      out <- tryCatch(irr(x), error = function(e) NULL)
      return(out)
    }
    
  2. For the error (or warning) to work, you actually need to create a function. I originally for error part just wrote error = return(NULL) and ALL values came back null.

  3. Remember to create a sub-output (like my "out") and to return(out).

Rashidarashidi answered 25/9, 2016 at 18:55 Comment(1)
Why is number 3 necessary?Beefwitted
M
4

The purrr package gives alternate functions that can be friendlier to set up than tryCatch. From the ?safely documentation, they are described as:

  • safely: wrapped function instead returns a list with components result and error. If an error occurred, error is an error object and result has a default value (otherwise). Else error is NULL.

  • quietly: wrapped function instead returns a list with components result, output, messages and warnings.

  • possibly: wrapped function uses a default value (otherwise) whenever an error occurs.

Note that these functions, unlike tryCatch(), are expected to wrap a function, not an expression, and they return a modified function. For OP's problem as stated, we would probably use possibly and wrap readLines directly to modify it.

url <- c(
    "http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html",
    "http://en.wikipedia.org/wiki/Xz", 
    "xxx")

library(purrr)
lapply(url, possibly(readLines, otherwise = "web URL is wrong, can't get"))
## with possibly, the error prints as a warning
## and the final value is the `otherwise` string

But also note that we could create a modified version of readLines, such as my_readLines <- possibly(readLines, otherwise = "web URL is wrong, can't get") that could be used in multiple places in our code.

I illustrate with possibly above, but we could easily imagine cases where we would want to use safely() (after which we could extract the result component from each list item, possibly skipping or otherwise handling items with a non-empty error component, perhaps even handling them differently based on the error), or quietly which also captures warnings and messages separately.

Monostich answered 1/5, 2023 at 15:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.