这与其他一些问题有关,但我似乎无法弄清楚如何应用答案,所以我问了一个新问题。
我试图从一段看起来像这样的代码中找出一个无意义的错误:
tryCatch(MainLoop(),
error=function(e) { fatal(lgr, paste('caught fatal error:', as.character(e)));
exit.status <<- 1 })
问题是该错误似乎与隐藏在库函数中的某些内容有关:
Error in nrow(x): (subscript) logical subscript too long
这nrow
不在我的代码中,因为上面的 C 级错误仅适用于我的任何nrow
调用中都不会发生的索引类型。
所以我真的很想从里面得到一个堆栈跟踪tryCatch
。这是一个类似的问题:
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
吗?有什么更好的方法?