[R] on abort error, always show call stack?
Henrik Bengtsson
hb at stat.berkeley.edu
Sun Aug 22 20:19:41 CEST 2010
For what's it worth, the Exception class of R.oo extends the
simpleError class. When an Exception object is instantiated it also
records the stack trace ("traceback()"). Then when the exception is
"thrown" it will behave just as stop(). For the convenience,
throw("message") does all this in one go, cf. stop(). You can use
throw() wherever you use stop(). Then, when an Exception object is
print():ed, it also displays the traceback.
Example:
> library("R.oo");
Loading required package: R.methodsS3
R.methodsS3 v1.2.0 (2010-03-13) successfully loaded. See ?R.methodsS3 for help.
R.oo v1.7.3 (2010-06-04) successfully loaded. See ?R.oo for help.
> bar <- function() throw("Woops!")
> foo <- function() bar()
> foo()
Error in list(`foo()` = <environment>, `bar()` = <environment>, `throw("Woops!")
` = <environment>, :
[2010-08-22 11:05:09] Exception: Woops!
at throw(Exception(...))
at throw.default("Woops!")
at throw("Woops!")
at bar()
at foo()
The neat thing with always getting the traceback is that almost always
people will cut'n'paste it when report errors/asking questions, which
avoids standard replied asking for "Please rerun and when you get the
error write traceback() and report back.". I haven't done it yet, but
you imagine adding an global option() where the sessionInfo() is also
printed, an additional message referring to webpage etc.
/Henrik
On Sun, Aug 22, 2010 at 8:41 AM, ivo welch <ivo.welch at gmail.com> wrote:
> Dear R Wizards---is it possible to get R to show its current call
> stack (sys.calls()) upon an error abort? I don't use ESS for
> execution, and it is often not obvious how to locate how I triggered
> an error in an R internal function. Seeing the call stack would make
> this easier. (right now, I sprinkle "cat" statements everywhere, just
> to locate the line where the error appears.) Of course, I would
> really love to see the line in my program that triggered this, but I
> have asked this before, and I understand this is too difficult to get
> into the R language.
>
> regards,
>
> /iaw
>
> ----
> Ivo Welch (ivo.welch at brown.edu, ivo.welch at gmail.com)
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list