| throw {R.oo} | R Documentation |
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 seperator sep="".
In other words, instead of doing:
stop(paste("Value out of range: ", value, ".", sep=""))
it is shorter to 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 specific classes, which can
then be caught (or not) using tryCatch().
## Default S3 method: throw(...)
... |
One or several strings that are concatenated and collapsed into on message string. |
Returns nothing.
Henrik Bengtsson http://www.braju.com/R/
See the Exception class for more detailed information.
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) {}
)