try_either: Try and catch_either.

View source: R/exception.R

try_eitherR Documentation

Try and catch_either.

Description

Try and catch_either.

Usage

try_either(expr, alt_value, catchfn = NULL, silent = TRUE)

Arguments

expr

Function which should be executed.

alt_value

return value when expr throw exception.

catchfn

Function which will be called when expr throw exception with error message as argument.

silent

Logical value: if TRUE, the error message is not shown.

Examples

f = function(i){
   if(i<0)stop("negative value")
   return(i)
}
x = try_either(f(3),NULL)
#x == 3
a = try_either(f(-1),NULL)
#a == NULL
b = try_either(f(-1),0)
#b == 0

hmito/hmRLib documentation built on Sept. 8, 2024, 4:49 p.m.