Using R — Basic error Handing with tryCatch()

This entry is part 4 of 21 in the series Using R

The R language definition section on Exception Handling describes a very few basics about exceptions in R but is of little use to anyone trying to write robust code that can recover gracefully in the face of errors. In fact, if you do a little searching you will find that quite a few people have read through the ?tryCatch documentation but come away just as confused as when they started. In this post we’ll try to clarify a few things and describe how R’s error handling functions can be used to write code that functions similarly to Java’s try-catch-finally construct.

List of error handling functions

Without any simple documentation on the subject, the first thing we need is a list of the functions involved in error handling. With this list in hand we can then start up R and type ?function_of_interest to read associated documentation or function_of_interest [without the ‘()’] to see how the function is implemented. Here is a minimal list of functions that anyone writing error handling code should read up on:

  • warning(…) — generates warnings
  • stop(…) — generates errors
  • suppressWarnings(expr) — evaluates expression and ignores any warnings
  • tryCatch(…) — evaluates code and assigns exception handlers

Other functions exist that relate to error handling but the above are enough to get started. (The documentation for these functions will lead to all the other error-related functions for any RTFM enthusiasts.)

R does try-catch-finally differently

In case you hadn’t noticed, R does a lot of things differently from most other programming languages. Java and Python and C and all other languages covered in Wikipedia’s excellent page on Exception handling syntax use language statements to enable try-catch-finally. R, needing to be different, uses a function.

But the tryCatch() function actually looks a lot like other languages’ try-catch syntax if you format it properly:

result = tryCatch({
}, warning = function(w) {
}, error = function(e) {
}, finally = {

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. Excerpting relevant text from the ?tryCatch documentation:

If a condition is signaled while evaluating ‘expr’ then [...] control
is transferred to the ‘tryCatch’ call that established the handler
[...] and the result returned by the handler is returned as the value
of the ‘tryCatch’ call. [...] The ‘finally’ expression is then
evaluated in the context in which ‘tryCatch’ was called.

What this means is that ‘expr’ is evaluated a line at a time until a ‘condition’ is encountered and then execution is transferred to the handler with the state in tact. Code can often explain more than words and the example at the end of this post is a standalone R script that explores various features that might be required in a robust error handling system:

  • generating warnings and errors from within a function
  • setting warning and error handlers with tryCatch()
  • providing alternative return values when a function generates a warning or error
  • modifying the text of warning and error messages
  • suppressing warning messages

Just copy and paste the script at the end, make it executable and try it out with the following commands:

$ chmod +x tryCatch.Rscript
$ ./tryCatch.r 1
$ ./tryCatch.r 0
$ ./tryCatch.r a
$ ./tryCatch.r
$ ./tryCatch.r warning
$ ./tryCatch.r error
$ ./tryCatch.r suppress-warnings

Pay special attention to what happens with ‘suppress-warnings’.

The are a couple of take home messages that result from this experimentation:

  1. tryCatch() isn’t that hard to use (Once  you know how!)
  2. warning() and stop() messages are accessible to the condition handlers.
  3. Do appropriate type conversion before passing arguments to functions.
  4. Ideally, the tryCatch() expression should be a single function.

And here is the tryCatch.Rscript example script. Happy error handling!

#!/usr/bin/env Rscript
# tryCatch.Rscript -- experiments with tryCatch

# Get any arguments
arguments <- commandArgs(trailingOnly=TRUE)
a <- arguments[1]

# Define a division function that can issue warnings and errors
myDivide <- function(d, a) {
  if (a == 'warning') {
    return_value <- 'myDivide warning result'
    warning("myDivide warning message")
  } else if (a == 'error') {
    return_value <- 'myDivide error result'
    stop("myDivide error message")
  } else {
    return_value = d / as.numeric(a)

# Evalute the desired series of expressions inside of tryCatch
result <- tryCatch({

  b <- 2
  c <- b^2
  d <- c+2
  if (a == 'suppress-warnings') {
    e <- suppressWarnings(myDivide(d,a))
  } else {
    e <- myDivide(d,a) # 6/a
  f <- e + 100

}, warning = function(war) {

  # warning handler picks up where error was generated
  print(paste("MY_WARNING:  ",war))
  b <- "changing 'b' inside the warning handler has no effect"
  e <- myDivide(d,0.1) # =60
  f <- e + 100

}, error = function(err) {

  # error handler picks up where error was generated
  print(paste("MY_ERROR:  ",err))
  b <- "changing 'b' inside the error handler has no effect"
  e <- myDivide(d,0.01) # =600
  f <- e + 100

}, finally = {

  print(paste("a =",a))
  print(paste("b =",b))
  print(paste("c =",c))
  print(paste("d =",d))
  # NOTE:  Finally is evaluated in the context of of the inital
  # NOTE:  tryCatch block and 'e' will not exist if a warning
  # NOTE:  or error occurred.
  #print(paste("e =",e))

}) # END tryCatch

print(paste("result =",result))
Series NavigationUsing R — A Script Introduction to RUsing R — Package Installation Problems
This entry was posted in R and tagged , . Bookmark the permalink.

6 Responses to Using R — Basic error Handing with tryCatch()

  1. Songpants says:

    Great post!

    Wish I would have had it when I tackled my first tryCatch() approaches and came back quite puzzled from the help page – glad to hear I’m apparently not the only one that struggled with it ;-)

    Best regards!

  2. DavidC says:

    This is nice, thanks. I think a lot of people using R (including me, until recently) don’t have enough education/experience programming to be thinking about error-handling.

  3. alex says:

    very useful, many thank’s
    i want to ask you: what plugin you use to write the R code in wordpress.

  4. datafireball says:

    I am writing an Hadoop Streaming job using R as the language for reducer, I found it super helpful to understand how tryCatch works and all the outputs are related with stdin, stdout and stderr.

    Great tutorial and thanks a lot!

  5. Vitalina says:

    I have a question about the situation when a is NA: $ ./tryCatch.r

    In this case we get the following result:

    Rscript tryCatch.R
    [1] “MY_ERROR: Error in if (a == \”suppress-warnings\”) {: missing value where TRUE/FALSE needed\n”
    [1] “a = NA”
    [1] “b = 2”
    [1] “c = 4”
    [1] “d = 6”
    [1] “result = 700”

    The reason for that is that a (which is NA) is being compared to “suppress-warnings”

    in the if block in myDivide function. The effect of this is that we deal with a = NA and still

    generate a value for f. However, a condition in an if statement should evaluate to a single

    TRUE or FALSE. I believe that using if (identical(a, “suppress-warnings”)) is a better way (

    it produces FALSE when a = NA). However, it seems that we will need a mechanism to deal with

    a when it is not defined.