[Rd] Alternative to .Internal(.dfltStop(msg, call))?

Henrik Bengtsson hb at biostat.ucsf.edu
Wed Feb 29 20:11:03 CET 2012


Hi,

I'm looking for a way to generate a "full stop" that will not be
caught by signal handlers, cf. .Internal(.dfltStop(msg, call)).

RATIONALE:
In the R.oo package I have throw() which (leaving out some details)
basically does what stop() does but appends a stack trace string to
the error message that shows up at the R prompt.  Example:

bar <- function() throw("A foo error");
foo <- function() bar();
> foo()
Error: A foo error
at foo()
at bar()

versus

bar <- function() stop("A foo error");
foo <- function() bar();
> stop("A foo error")
Error: A foo error


CURRENT SOLUTION:
throw() does this in a way such that the stack trace is *not* part of
the getMessage() of the 'error' condition object, which is important
when catching the error in signal handlers. Here is a stub what is
done, leaving out the details how to retrieve the stack trace (call
history):

throw <- function(msg, ...) {
  cond <- simpleError(msg);
  signalCondition(cond);

  # A dummy stracktrack / call history
  stackTraceStr <- "at foo()\nat bar()\n";
  call <- NULL; # Also a dummy for this example

   # If the condition is not caught, do a "full stop" showing error
message and stacktrace
  fullmsg <- paste(msg, "\n", stackTraceStr, sep="");
 .Internal(.dfltStop(fullmsg, call));
} # throw()


QUESTION:
Is it possible to achieve this without using an .Internal() call?
Note that stop() will use the same message string for both
signalCondition() and .Internal(.dfltStop(...)), cf.

> base::stop
function (..., call. = TRUE, domain = NULL)
{
...
        message <- conditionMessage(cond)
        call <- conditionCall(cond)
        .Internal(.signalCondition(cond, message, call))
        .Internal(.dfltStop(message, call))
...
}


ALTERNATIVE?:
Is there an alternative way to have the stack trace/traceback() being
automatically displayed after the error message being displayed at the
prompt?


/Henrik



More information about the R-devel mailing list