获取 R 中 tryCatch 错误的堆栈跟踪 [英] get stack trace on tryCatch'ed error in R

查看:29
本文介绍了获取 R 中 tryCatch 错误的堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与其他一些问题有关,但我似乎无法弄清楚如何应用答案,所以我提出了一个新问题.

This is related to some other questions, but I can't seem to figure out how to apply the answer, so I'm asking a new question.

我试图从如下所示的一段代码中找出一个没有信息量的错误:

I'm trying to figure out an uninformative error from a piece of code that looks like this:

tryCatch(MainLoop(), 
  error=function(e) { fatal(lgr, paste('caught fatal error:', as.character(e))); 
                      exit.status <<- 1 })

问题是该错误似乎与隐藏在库函数中的某些内容有关:

The problem is that the error appears to be related to something buried in a library function:

Error in nrow(x): (subscript) logical subscript too long

那个 nrow 不在我的代码中,因为上面的 C 级错误仅适用于在我的任何 nrow 调用中从未发生过的索引类型.

That nrow is not in my code, as the C-level error above only applies to a type of indexing that never happens in any of my nrow calls.

所以我真的很想从 tryCatch 中获取堆栈跟踪.这是一个类似的问题:

So I'd really like to get a stack trace from within that tryCatch. Here's an analogous problem:

x <- function() { y(); }
y <- function() { z(); }
z <- function() { stop("asdf") }

> x()
Error in z() : asdf
> tryCatch(x(), error=function(e) { print(conditionCall(e)) } )
z()
> tryCatch(x(), error=function(e) { dump.frames() } )
> last.dump
$`tryCatch(x(), error = function(e) {
    dump.frames()
})`
<environment: 0x1038e43b8>

$`tryCatchList(expr, classes, parentenv, handlers)`
<environment: 0x1038e4c60>

$`tryCatchOne(expr, names, parentenv, handlers[[1]])`
<environment: 0x1038e4918>

$`value[[3]](cond)`
<environment: 0x1038ea578>

attr(,"error.message")
[1] "asdf"
attr(,"class")
[1] "dump.frames"

如何获取包含对 y() 调用的堆栈跟踪?我必须停止使用 tryCatch 吗?什么是更好的方法?

How do I get the stack trace that includes the call to y()? Do I have to stop using tryCatch? What's a better way?

推荐答案

对于交互式使用,可以 trace(stop, quote(print(sys.calls()))) 打印调用堆栈在调用 stop() 时.

For interactive use one might trace(stop, quote(print(sys.calls()))) to print the call stack at the time stop() is invoked.

来自 ?tryCatch,

 The function 'tryCatch' evaluates its expression argument in a
 context where the handlers provided in the '...'  argument are
 available.

 Calling handlers are established by 'withCallingHandlers'...
 the handler is called... in the context where the condition
 was signaled...

所以

>     withCallingHandlers(x(), error=function(e) print(sys.calls()))
[[1]]
withCallingHandlers(x(), error = function(e) print(sys.calls()))

[[2]]
x()

[[3]]
y()

[[4]]
z()

[[5]]
stop("asdf")

[[6]]
.handleSimpleError(function (e) 
print(sys.calls()), "asdf", quote(z()))

[[7]]
h(simpleError(msg, call))

Error in z() : asdf

如果有一个内部的 tryCatch,这会被阻止

This is thwarted if there is an inner tryCatch

withCallingHandlers({
    tryCatch(x(), error=function(e) stop("oops"))
}, error=function(e) print(sys.calls()))

因为我们只有在 tryCatch '处理'错误后才能访问调用堆栈.

as we only have access to the call stack after the tryCatch has 'handled' the error.

这篇关于获取 R 中 tryCatch 错误的堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆