From sfalcon at fhcrc.org  Tue Oct 18 18:06:22 2005
From: sfalcon at fhcrc.org (Seth Falcon)
Date: Tue, 18 Oct 2005 09:06:22 -0700
Subject: [Rd] tryCatch, simpleCondition question
Message-ID: <m2k6ga24jl.fsf@macaroni.local>

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



[Rd] tryCatch, simpleCondition question
Seth Falcon