tryCatch2: Condition handling and recovery

View source: R/utils.R

tryCatch2R Documentation

Condition handling and recovery

Description

Modification of tryCatch which allows recovery from warnings and messages while returning the value from expr. Note that the return value from errors and interruptions is NULL.

Usage

tryCatch2(expr, ..., simplify = TRUE)

Arguments

expr

an expression

...

ignored

simplify

logical; if TRUE, simplifies the returned list to a vector if there were no exceptions

Value

If expr is evaluated with no errors, warnings, messagess, or interruptions, then only the value of expr is returned unless simplify = FALSE in which case a list with the return value.

If expr results in one of the above conditions, then a list with the value of expr along with an additional element for the condition(s) which can be accessed with $warning, $error, etc.

Author(s)

Martin Morgan, Jan Gorecki, Robert Redd (modifications)

See Also

tryCatch; logR::tryCatch2; https://stackoverflow.com/q/4948361/2994949; logR github repo

Examples

## returns value if no errors, warnings, etc
tryCatch2(1)
tryCatch2(1, simplify = FALSE)


tryCatch2(stop('halt at once!'))
tryCatch2({warning('warning'); message('message'); 1})


## compare
tryCatch({warning('this is your warning...'); 1}, warning = function(w) w)
tryCatch2({warning('this is your warning...'); 1})

tryCatch({message('calculating...'); 1}, message = function(m) m)
tryCatch2({message('calculating...'); 1})


raredd/rawr documentation built on March 4, 2024, 1:36 a.m.