ID EN
Vector & List

conditions

R Base 3.6.2

These functions provide a mechanism for handling unusual conditions, including errors and warnings.

Syntax

R
tryCatch(expr, …, finally)
withCallingHandlers(expr, &#8230;)<p></p><p>signalCondition(cond)</p><p>simpleCondition(message, call = NULL)
simpleError    (message, call = NULL)
simpleWarning  (message, call = NULL)
simpleMessage  (message, call = NULL)</p><p>errorCondition(message, ..., class = NULL, call = NULL)
warningCondition(message, ..., class = NULL, call = NULL)</p><p># S3 method for condition
as.character(x, &#8230;)
# S3 method for error
as.character(x, &#8230;)
# S3 method for condition
print(x, &#8230;)
# S3 method for restart
print(x, &#8230;)</p><p>conditionCall(c)
# S3 method for condition
conditionCall(c)
conditionMessage(c)
# S3 method for condition
conditionMessage(c)</p><p>withRestarts(expr, &#8230;)</p><p>computeRestarts(cond = NULL)
findRestart(name, cond = NULL)
invokeRestart(r, &#8230;)
invokeRestartInteractively(r)</p><p>isRestart(x)
restartDescription(r)
restartFormals(r)</p><p>suspendInterrupts(expr)
allowInterrupts(expr)</p><p>.signalSimpleWarning(msg, call)
.handleSimpleError(h, msg, call)
.tryResumeInterrupt()</p>

Arguments

Parameter Description
c a condition object.
call call expression.
cond a condition object.
expr expression to be evaluated.
finally expression to be evaluated before returning or exiting.
h function.
message character string.
msg character string.
name character string naming a restart.
r restart object.
x object.
class character string naming a condition class.
&#8230; additional arguments; see details below.

Details

The condition system provides a mechanism for signaling and handling unusual conditions, including errors and warnings. Conditions are represented as objects that contain information about the condition that occurred, such as a message and the call in which the condition occurred. Currently conditions are S3-style objects, though this may eventually change. Conditions are objects inheriting from the abstract class condition. Errors and warnings are objects inheriting from the abstract subclasses error and warning. The class simpleError is the class used by stop and all internal error signals. Similarly, simpleWarning is used by warning, and simpleMessage is used by message. The constructors by the same names take a string describing the condition as argument and an optional call. The funct

Examples

Example
R
# NOT RUN {
tryCatch(1, finally = print("Hello"))
e <- simpleError("test error")
# }
# NOT RUN {
 stop(e)
 tryCatch(stop(e), finally = print("Hello"))
 tryCatch(stop("fred"), finally = print("Hello"))
# }
# NOT RUN {
tryCatch(stop(e), error = function(e) e, finally = print("Hello"))
tryCatch(stop("fred"),  error = function(e) e, finally = print("Hello"))
withCallingHandlers({ warning("A"); 1+2 }, warning = function(w) {})
# }
# NOT RUN {
 { withRestarts(stop("A"), abort = function() {}); 1 }
# }
# NOT RUN {
withRestarts(invokeRestart("foo", 1, 2), foo = function(x, y) {x + y})

##--> More examples are part of
##-->   demo(error.catching)
# }

See Also

stop and warning signal conditions and try is essentially a simplified version of tryCatch. assertCondition in package tools tests that conditions are signalled and works with several of the above handlers.