throw: Throws an Exception

Description Usage Arguments Value Author(s) See Also Examples

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

1
2
## 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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
  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) {}
  )

Example output

Loading required package: R.methodsS3
R.methodsS3 v1.7.1 (2016-02-15) successfully loaded. See ?R.methodsS3 for help.
R.oo v1.22.0 (2018-04-21) successfully loaded. See ?R.oo for help.

Attaching package: 'R.oo'

The following objects are masked from 'package:methods':

    getClasses, getMethods

The following objects are masked from 'package:base':

    attach, detach, gc, load, save

 [1] 0 0 1 0 1 0 0 0 0 0
NULL

R.oo documentation built on Aug. 26, 2020, 5:13 p.m.