Last data update: 2014.03.03

R: 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 throw implementation is fully backward compatible with this one, but the error object thrown is of class Exception.

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) {})

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(R.methodsS3)
R.methodsS3 v1.7.1 (2016-02-15) successfully loaded. See ?R.methodsS3 for help.
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/R.methodsS3/throw.Rd_%03d_medium.png", width=480, height=480)
> ### Name: throw
> ### Title: Throws an exception
> ### Aliases: throw.default throw
> ### Keywords: error
> 
> ### ** 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 1 0 1 1 1 0 0 1
> # [1] 0 1 0 0 0 1 0 0 1 0
> tryCatch({
+   rbern(10, 10*0.4)
+ }, error=function(ex) {})
NULL
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>