Difficulty using the tryCatch() function

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Difficulty using the tryCatch() function

phil-3
I need help using the tryCatch function. I have a function and I want to
surround it with tryCatch to catch errors and thereby avoid stopping
execution of my program if the function fails. In my reproducible
example below I have used a very simply function that just adds two
numbers together. My function, called Adn, compiles, but when executed
it generates the message: "Error in tryCatch({ : condition handlers must
be specified with a condition class". I do not understand the error
message.

# Reproducible example for using tryCatch

Adn <- function(x,y) {
   out <- tryCatch(
     {
       x+y
     },
     {
       warning = function(cond) {
         message("There was a warning.")
         message("Here is the original warning message:")
         message(cond)
         return(100)
       }
     },
     {
       error = function(cond) {
         message("There was an error.")
         message("Here is the original error message:")
         message(cond)
         return(200)
       }
     },
     finally = {
       message("Error handling done.")
     }
   )
   if (out==100 | out==200) { z <- 0 }
   else { z <- x+y }
   return(z)
}
(result <- Adn(1,sqrt(2))) # should work fine
(result <- Adn(1,sqrt(-2))) # should catch a warning and set z to 0
(result <- Adn(1,"a")) # should catch an error and set z to 0

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Difficulty using the tryCatch() function

Jeff Newmiller
Too many curly braces. warning and error need to be arguments to tryCatch.

On February 7, 2021 11:30:59 AM PST, [hidden email] wrote:

>I need help using the tryCatch function. I have a function and I want
>to
>surround it with tryCatch to catch errors and thereby avoid stopping
>execution of my program if the function fails. In my reproducible
>example below I have used a very simply function that just adds two
>numbers together. My function, called Adn, compiles, but when executed
>it generates the message: "Error in tryCatch({ : condition handlers
>must
>be specified with a condition class". I do not understand the error
>message.
>
># Reproducible example for using tryCatch
>
>Adn <- function(x,y) {
>   out <- tryCatch(
>     {
>       x+y
>     },
>     {
>       warning = function(cond) {
>         message("There was a warning.")
>         message("Here is the original warning message:")
>         message(cond)
>         return(100)
>       }
>     },
>     {
>       error = function(cond) {
>         message("There was an error.")
>         message("Here is the original error message:")
>         message(cond)
>         return(200)
>       }
>     },
>     finally = {
>       message("Error handling done.")
>     }
>   )
>   if (out==100 | out==200) { z <- 0 }
>   else { z <- x+y }
>   return(z)
>}
>(result <- Adn(1,sqrt(2))) # should work fine
>(result <- Adn(1,sqrt(-2))) # should catch a warning and set z to 0
>(result <- Adn(1,"a")) # should catch an error and set z to 0
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Difficulty using the tryCatch() function

Greg Minshall
In reply to this post by phil-3
try removing the outermost '{'...'}'.  e.g., { warning = ... } should be
'warning = ...'.
----
Adn <- function(x,y) {
    out <- tryCatch(
    {
        x+y
    },
    warning = function(cond) {
        message("There was a warning.")
        message("Here is the original warning message:")
        message(cond)
        return(100)
    },
    error = function(cond) {
        message("There was an error.")
        message("Here is the original error message:")
        message(cond)
        return(200)
    },
    finally = {
        message("Error handling done.")
    }
    )
    if (out==100 | out==200) { z <- 0 }
    else { z <- x+y }
    return(z)
}
(result <- Adn(1,sqrt(2))) # should work fine
(result <- Adn(1,sqrt(-2))) # should catch a warning and set z to 0
(result <- Adn(1,"a")) # should catch an error and set z to 0

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Difficulty using the tryCatch() function

phil-3
In reply to this post by Jeff Newmiller
Thanks so much for your speedy replies. Yes, removing those brackets did
the trick. I was relying on an example in Stackoverflow at
https://stackoverflow.com/questions/12193779/how-to-write-trycatch-in-r

Philip

On 2021-02-07 14:36, Jeff Newmiller wrote:

> Too many curly braces. warning and error need to be arguments to
> tryCatch.
>
> On February 7, 2021 11:30:59 AM PST, [hidden email] wrote:
>> I need help using the tryCatch function. I have a function and I want
>> to
>> surround it with tryCatch to catch errors and thereby avoid stopping
>> execution of my program if the function fails. In my reproducible
>> example below I have used a very simply function that just adds two
>> numbers together. My function, called Adn, compiles, but when executed
>> it generates the message: "Error in tryCatch({ : condition handlers
>> must
>> be specified with a condition class". I do not understand the error
>> message.
>>
>> # Reproducible example for using tryCatch
>>
>> Adn <- function(x,y) {
>>   out <- tryCatch(
>>     {
>>       x+y
>>     },
>>     {
>>       warning = function(cond) {
>>         message("There was a warning.")
>>         message("Here is the original warning message:")
>>         message(cond)
>>         return(100)
>>       }
>>     },
>>     {
>>       error = function(cond) {
>>         message("There was an error.")
>>         message("Here is the original error message:")
>>         message(cond)
>>         return(200)
>>       }
>>     },
>>     finally = {
>>       message("Error handling done.")
>>     }
>>   )
>>   if (out==100 | out==200) { z <- 0 }
>>   else { z <- x+y }
>>   return(z)
>> }
>> (result <- Adn(1,sqrt(2))) # should work fine
>> (result <- Adn(1,sqrt(-2))) # should catch a warning and set z to 0
>> (result <- Adn(1,"a")) # should catch an error and set z to 0
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.