throw: Throws an exception

throwR Documentation

Throws an exception

Description

Throws an exception by calling stop().

Note that throw() can be defined for specific classes, which can then be caught (or not) using tryCatch().

This default function will be overridden by ditto in the R.oo package, if that is loaded. The latter R.oo::throw() implementation is fully backward compatible with this one, but the error object thrown is of class R.oo::Exception.

WARNING: This function is deprecated in favor of R.oo::throw(), or alternatively, just stop().

Usage

## Default S3 method:
throw(...)

Arguments

...

One or several strings that are concatenated and collapsed into on message string.

Value

Returns nothing.

Author(s)

Henrik Bengtsson

Examples

rbern <- function(n=1, prob=1/2) {
  if (prob < 0 || prob > 1)
    throw("Argument 'prob' is out of range: ", prob)
  rbinom(n=n, size=1, prob=prob)
}

rbern(10, 0.4)
# [1] 0 1 0 0 0 1 0 0 1 0
tryCatch({
  rbern(10, 10*0.4)
}, error=function(ex) {})

R.methodsS3 documentation built on June 14, 2022, 1:06 a.m.