Using tryCatch Function to Handle Errors & Warnings in R (3 Examples) (2024)

In this article you’ll learn how to debug R codes using the tryCatch function in the R programming language.

The tutorial will consist of this content:

1) Basic Explanation of the tryCatch() Function

2) Example 1: Executing tryCatch() Function without Warnings or Errors

3) Example 2: Executing tryCatch() Function with Error

4) Example 3: Executing tryCatch() Function with Warning

Let’s start right away:

Basic Explanation of the tryCatch() Function

The tryCatch function checks whether an R code leads to an error or warning message. Hence, the tryCatch function is often used to debug R codes.

Within the tryCatch function, we usually should specify four arguments:

  • expr: This specifies the expression we want to evaluate.
  • error: The message we want to return in case of an error.
  • warning: The message we want to return in case of a warning.
  • finally: The message we want to return when the tryCatch function is finished.

Note that only the expr argument is mandatory. However, I recommend to specify the other arguments as well to produce meaningful outputs in case an error or warning message appears.

Let’s do this in practice…

Example 1: Executing tryCatch() Function without Warnings or Errors

The following example shows how to apply the tryCatch function to a properly specified expression in R. For this, we are using the expression 1 + 1.

tryCatch( # Applying tryCatch expr = { # Specifying expression 1 + 1 message("Everything was fine.") }, error = function(e){ # Specifying error message message("There was an error message.") }, warning = function(w){ # Specifying warning message message("There was a warning message.") }, finally = { # Specifying final message message("tryCatch is finished.") })# Everything was fine.# tryCatch is finished.

The RStudio console returns the messages “Everything was fine.” and “tryCatch is finished.”, indicating that our expression didn’t have any problems.

Example 2: Executing tryCatch() Function with Error

The following syntax shows the application of tryCatch to a falsely specified expression. Let’s assume that we are trying to execute the expression 1 + “1” (i.e. the second “1” has the character class). Then, the tryCatch function returns the following output:

tryCatch( # Applying tryCatch expr = { # Specifying expression 1 + "1" message("Everything was fine.") }, error = function(e){ # Specifying error message message("There was an error message.") }, warning = function(w){ # Specifying warning message message("There was a warning message.") }, finally = { # Specifying final message message("tryCatch is finished.") })# There was an error message.# tryCatch is finished.

“There was an error message.” – We clearly have done something wrong in our R code.

Note that, for the sake of simplicity of this example, we are only returning a message to the RStudio console telling us that an error occurred.

However, you may use much more complex R codes to handle errors with the tryCatch function. You simply need to replace the error argument by the handler you want to use.

Example 3: Executing tryCatch() Function with Warning

Similar to the previous example, Example 3 shows how to apply the tryCatch command to an expression that returns a warning message. For this example, we’ll use the expression 1:2 + 1:3.

tryCatch( # Applying tryCatch expr = { # Specifying expression 1:2 + 1:3 message("Everything was fine.") }, error = function(e){ # Specifying error message message("There was an error message.") }, warning = function(w){ # Specifying warning message message("There was a warning message.") }, finally = { # Specifying final message message("tryCatch is finished.") })# There was a warning message.# tryCatch is finished.

This time the tryCatch function returned our manually specified warning message.

Video & Further Resources

Do you want to know more about tryCatch? Then you may have a look at the following video tutorial of my YouTube channel. In the video instruction, I’m illustrating the R syntax of the present tutorial.

The YouTube video will be added soon.

Furthermore, you might read the related articles of this website.

  • Useful Commands in R (Examples)
  • All R Programming Tutorials

In this article, I illustrated how to write a tryCatch in the R programming language. Let me know in the comments, in case you have any additional questions.

4 Comments. Leave new

  • Using tryCatch Function to Handle Errors & Warnings in R (3 Examples) (1)

    Kat Alex

    January 10, 2022 9:31 am

    The expression like:

    warning = function(w){
    message(w$message)

    returns warning and error messages

    Reply
    • Using tryCatch Function to Handle Errors & Warnings in R (3 Examples) (2)

      Joachim

      January 10, 2022 9:57 am

      Hey Kat,

      Could you explain this in some more detail? I’m not sure if I get what you want to say.

      Regards,
      Joachim

      Reply
  • Using tryCatch Function to Handle Errors & Warnings in R (3 Examples) (3)

    Kat Alex

    January 11, 2022 9:24 am

    I am not sure if it is important. But sometimes it is necessary to see what exactly was wrong:
    Input:

    tryCatch( # Applying tryCatch

    expr = { # Specifying expression
    1 + ‘u’
    message(“Everything was fine.”)
    },

    error = function(e){ # Specifying error message
    message(e$message)
    },

    warning = function(w){ # Specifying warning message
    message(w$message)
    },

    finally = { # Specifying final message
    message(“work is finished.”)
    }
    )

    Output:

    non-numeric argument to binary operator
    work is finished.

    Reply
    • Using tryCatch Function to Handle Errors & Warnings in R (3 Examples) (4)

      Joachim

      January 11, 2022 4:41 pm

      Ah I see. Thanks for sharing, this is actually quite useful! 🙂

      Reply

Leave a Reply

I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.

Statistics Globe Newsletter

Related Tutorials

R Error in as.Date.numeric(X) : ‘origin’ must be supplied (2 Examples)

R Error: Object of Type Closure is not Subsettable in R (2 Examples)

Using tryCatch Function to Handle Errors & Warnings in R (3 Examples) (2024)

References

Top Articles
Latest Posts
Article information

Author: Dan Stracke

Last Updated:

Views: 5836

Rating: 4.2 / 5 (63 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Dan Stracke

Birthday: 1992-08-25

Address: 2253 Brown Springs, East Alla, OH 38634-0309

Phone: +398735162064

Job: Investor Government Associate

Hobby: Shopping, LARPing, Scrapbooking, Surfing, Slacklining, Dance, Glassblowing

Introduction: My name is Dan Stracke, I am a homely, gleaming, glamorous, inquisitive, homely, gorgeous, light person who loves writing and wants to share my knowledge and understanding with you.