Learn R Programming

FastRet (version 1.3.0)

withStopMessage: Try expression with predefined error message

Description

Executes an expression and prints an error message if it fails

Usage

withStopMessage(expr)

Value

The result of the expression

Arguments

expr

The expression to execute

Examples

Run this code
f <- function(expr) {
  val <- try(expr, silent = TRUE)
  err <- if (inherits(val, "try-error")) attr(val, "condition") else NULL
  if (!is.null(err)) value <- NULL
  list(value = val, error = err)
}
ret <- f(log("a")) # this error will not show up in the console
ret <- f(withStopMessage(log("a"))) # this error will show up in the console

Run the code above in your browser using DataLab