stop
Error and Warning Messages
Description
Issues an error or warning message from a function.
Additionally, stop causes the function to be exited.
Usage
stop(..., call. = TRUE, domain = NULL)
warning(..., call. = TRUE, immediate. = FALSE, domain = NULL)
suppressWarnings(expr, classes = "warning")
suppressUnimplementedGraphicsWarnings(expr, call = sys.call(-1), showCount = TRUE)
geterrmessage()
Arguments
... |
zero or more objects that can be converted to characters, or one single
condition object.
|
call. |
a logical flag. If TRUE (the default), the call is added to the
stop or warning message.
|
domain |
The domain specifying the language for translating the message. If
NA, messages are not translated.
|
immediate. |
a logical flag. If TRUE, the warning is output immediately. The
default is FALSE.
|
expr |
the expression to evaluate.
|
classes |
a vector of class names.
|
call |
a call object. The first few characters of the text representation of
this call will be put after "Warning message: In" when printing the
warning message. The default is the call to the caller of suppressUnimplementedGraphicsWarnings.
|
showCount |
a logical value. If TRUE (the default) and there were some unimplemented-graphics warnings,
suppressUnimplementedGraphicsWarnings will emit a warning saying how many there were.
Otherwise it will totally suppress unimplemented-graphics warnings.
|
Details
- stop signals an error with a specified error message. The
normal error handling process:
- Prints the name of the function calling stop followed
by the error message.
- Saves the call stack at the point of the error.
- Terminates execution and returns to the top-level console.
The saved call stack can be printed
by calling traceback. It is possible to trap errors and
continue execution by calling try. The error
option (see options) specifies the action to take when
an error occurs.
- warning prints the specified warning message, but it does not
stop execution. The warn option (see options)
controls the behavior when a warning is encountered as follows:
- warn=-1: Ignores the warning message.
- warn=0: Saves the message. Saved messages are printed at
the end of the top level expression execution, or by calling the
function warnings.
- warn=1: Prints the message immediately.
- warn=2: Causes the warning to behave as an error.
- suppressWarnings evaluates the expression expr, but it
causes any warnings resulting from the evaluation to be ignored if the
warning condition object class contains any of the classes in classes.
- suppressUnimplementedGraphicsWarnings evaluates the expression expr, but it
causes any warnings about unimplemented graphics functions resulting from the evaluation to be ignored.
Calling options(suppressUnimplementedWarnings = TRUE) will suppress the production of such warnings.
(Spotfire Enterprise Runtime for R has not implemented a graphics system but does supply functions in package:graphics
and elsewhere to allow graphics code in other packages to run, but they produce
no graphics and emit a warning that the functions are not implemented.)
It is possible to specify more complex behavior when an error or
warning occurs using the condition system, via functions such as
withCallingHandlers and
tryCatch. If a
condition object is passed as the only argument to
stop or
warning, it is used to signal the event, rather than
constructing a
simpleError or
simpleWarning condition
object.
Value
stop | does not have a return value because it terminates executing the current expression. |
warning | invisibly returns the warning message as a string. |
suppressWarnings | returns the value of expr. |
suppressUnimplementedGraphicsWarnings | returns the value of expr. |
geterrmessage | returns the last error message, or the empty
string if there is no error occured. |
References
Becker, R. A., Chambers, J. M., and Wilks, A. R. 1988. The New S Language. Pacific Grove, CA: Wadsworth & Brooks/Cole Advanced Books and Software.
See Also
Examples
# save any warnings
options(warn=0)
# generate warnings
x <- seq(-5,5) # positive and negative numbers
srx <- sqrt(x)
if (any(is.nan(srx))) warning("srx contains NaNs!")
# print saved warnings
warnings()
# generate error
M <- matrix(1:4, nrow=2)
if (!isSymmetric(M)) {
stop("The matrix M is not symmetric!")
}
# get last error message
geterrmessage()