# NOT RUN {
# Creating a condition of type "foo"
cnd <- cnd("foo")
# If no handler capable of dealing with "foo" is established on the
# stack, signalling the condition has no effect:
cnd_signal(cnd)
# To learn more about establishing condition handlers, see
# documentation for with_handlers(), exiting() and inplace():
with_handlers(cnd_signal(cnd),
foo = inplace(function(c) cat("side effect!\n"))
)
# By default, cnd_signal() creates a muffling restart which allows
# inplace handlers to prevent a condition from being passed on to
# other handlers and to resume execution:
undesirable_handler <- inplace(function(c) cat("please don't call me\n"))
muffling_handler <- inplace(function(c) {
cat("muffling foo...\n")
rst_muffle(c)
})
with_handlers(foo = undesirable_handler,
with_handlers(foo = muffling_handler, {
cnd_signal("foo")
"return value"
}))
# cnd_warn() and cnd_inform() signal a condition and display a
# warning or message:
# }
# NOT RUN {
cnd_inform(cnd)
cnd_warn(cnd)
# }
# NOT RUN {
# You can signal a critical condition with cnd_abort(). Unlike
# cnd_signal() which has no side effect besides signalling the
# condition, cnd_abort() makes the program terminate with an error
# unless a handler can deal with the condition:
# }
# NOT RUN {
cnd_abort(cnd)
# }
# NOT RUN {
# If you don't specify a .msg or .call, the default message/call
# (supplied to cnd()) are displayed. Otherwise, the ones
# supplied to cnd_abort() and cnd_signal() take precedence:
# }
# NOT RUN {
critical <- cnd("my_error",
.msg = "default 'my_error' msg",
.call = quote(default(call))
)
cnd_abort(critical)
cnd_abort(critical, .msg = "overridden msg")
fn <- function(...) {
cnd_abort(critical, .call = TRUE)
}
fn(arg = foo(bar))
# }
# NOT RUN {
# Note that by default a condition signalled with cnd_abort() does
# not have a muffle restart. That is because in most cases,
# execution should not continue after signalling a critical
# condition.
# }
Run the code above in your browser using DataLab