Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

throw

Throws an Exception


Description

Throws an exception similar to stop(), but with support for Exception classes. The first argument (object) is by default pasted together with other arguments (...) and with separator sep="". For instance, to throw an exception, write

throw("Value out of range: ", value, ".").

which is short for

throw(Exception("Value out of range: ", value, ".")).

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

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

See Also

See the Exception class for more detailed information.

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.oo

R Object-Oriented Programming with or without References

v1.24.0
LGPL (>= 2.1)
Authors
Henrik Bengtsson [aut, cre, cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.