[Rd] Is it possible to retrieve the last error? (not error *message*)

Henrik Bengtsson henrik.bengtsson at gmail.com
Wed May 4 22:38:45 CEST 2016


On Wed, May 4, 2016 at 1:27 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
>> On May 4, 2016, at 12:41 PM, Henrik Bengtsson <henrik.bengtsson at gmail.com> wrote:
>>
>> Hi,
>>
>> at the R prompt, is it possible to retrieve the last error (as in
>> condition object of class "error")?
>>
>> I'm not asking for geterrmessage(), which only returns the error
>> message (as a character string).  I'm basically looking for a
>> .Last.error or .Last.condition, analogously to .Last.value for values,
>> which can be used when it is "too late" (not possible) to go back an
>> use try()/tryCatch().
>
> After looking at the code for the exposed `traceback`
>>  I'm wondering if this delivers what you expect:
>
> .traceback()[1]

Thanks, but unfortunately not:

> stop("Hello")
Error: Hello
> ex <- .traceback()[1]
> str(ex)
List of 1
 $ : chr "stop(\"Hello\")"
> inherits(ex, "condition")
[1] FALSE


I'm looking for something that returns the object of class condition, cf.

> ex <- attr(try(stop("Hello")), "condition")
Error in try(stop("Hello")) : Hello
> str(ex)
List of 2
 $ message: chr "Hello"
 $ call   : language doTryCatch(return(expr), name, parentenv, handler)
 - attr(*, "class")= chr [1:3] "simpleError" "error" "condition"
> inherits(ex, "condition")
[1] TRUE


The reason is that this object may contain additional information that
is not available in the message nor the call.  For instance, a package
may define a richer error class that captures/carries more information
about an error (e.g. a time stamp, a remote session information, ...)
and which is therefore not available via neither geterrmessage() nor
traceback().

I am aware that I might be asking for something that is not supported
and requires that the default signal handlers be modified.

/Henrik

>
>
>> Thanks,
>>
>> Henrik
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> David Winsemius
> Alameda, CA, USA
>



More information about the R-devel mailing list