Learn R Programming

simsalapar (version 1.0-12)

tryCatch.W.E: Catching and Storing Warnings and Errors Simultaneously

Description

Catches and saves both warnings (warning) and errors (stop) and in the case of a warning, also the computed result.

Usage

tryCatch.W.E(expr)

Value

list with components

value

value of expr or error message (see simpleError or stop()).

warning

warning message (see simpleWarning or warning()) or NULL.

Arguments

expr

expression to be evaluated, typically a function call.

Author

Marius Hofert and Martin Maechler, based on hints from Luke Tierney and Bill Dunlap, see https://stat.ethz.ch/pipermail/r-help/2010-December/262626.html.

Details

This function is particularly useful in large(r) simulation studies to check all computations and guarantee their correctness.

References

see simsalapar-package.

See Also

the base function tryCatch() and demo(error.catching). Also, doCallWE(), of which tryCatch.W.E() is the “workhorse”.

Examples

Run this code
## Adapted from demo(error.catching) :
str(r1 <- tryCatch.W.E( log( 2 ) ) )
str(r2 <- tryCatch.W.E( log(-1 ) ) )
str(r3 <- tryCatch.W.E( log("a") ) )
stopifnot(is.null(r1$warning),
          is.na  (r2$value),   inherits(r2$warning, "warning"),
          is.null(r3$warning), inherits(r3$value, "error"))

Run the code above in your browser using DataLab