[Rd] tryCatch, simpleCondition question

Seth Falcon sfalcon at fhcrc.org
Tue Oct 18 18:06:22 CEST 2005


Hi,

I'm trying to learn how to use the tryCatch condition system and am
confused about the behavior when I set the call arg of a
simpleCondition.

I want to catch an error if it meets a certain criteria and otherwise
reraise it.  I want the error message to indicate the function in
which the error occured (and not mention tryCatch).

Wanted:

  > g("foo")         ## error caught
  [1] "caught foo"

  > g("otherwise")   ## error reraised
  Error in g("otherwise") : otherwise

This works:

g <- function(msg) {
    mycall <- sys.call(0)
    tryCatch(stop(msg),
             error=function(e) {
                 if ("foo" == conditionMessage(e))
                   "caught foo"
                 else {
                     cond <- simpleCondition(conditionMessage(e),
                                             call=mycall)
                     stop(cond)
                 }
             })
}

But I think I should be able to use sys.parent, or sys.call inside the
simpleCondition call instead of saving my position.

I found that call=sys.call(-4) gives me the desired result, but can't
imagine that this would be a good thing to put into code.  Is there a
convenient way to say, "give me the call one up from the tryCatch I'm
currently in"?  Or is saving my call to pass into the tryCatch the
best way?

Thanks,

+ seth



More information about the R-devel mailing list