处理 R 中的错误最好的是什么?我希望能够从我的脚本的最终用户那里抽象出堆栈跟踪,以及清理我可能正在使用的任何临时变量和状态。
所以我想我的问题有两个:
- 最重要的是,在处理错误时如何正确处理清理状态?
- 我如何从 R 的错误消息中提取有用的、好的信息,而不会吐出内部的东西?
目前,我有一些看起来像这样的东西,但是(1)它看起来非常不雅,并且(2)仍然给我可怕的错误信息。
# Create some temporary working state and variables to clean up
file <- "somefile"
working.dir <- getwd()
setwd("../") # Go somewhere else
saf.default <- getOption("stringsAsFactors")
options(stringsAsFactors = FALSE)
# Now lets try to open the file, but it doesn't work (for whatever reason)
# I want to clean up the state, stop, and wrap the error string with a nicer
# message
tryCatch({
# Need to embed the tryCatch because we still need some of the state variables
# for the error message
tryCatch({
f <- read.table(file)
}, error = function(err.msg) {
# Can't clean up here, we still need the `file variable!
stop("Could not open file '", file, "' with error message:\n", print(err.msg),
call.=FALSE)
})
}, error = function(err.msg) {
# Now we can clean up state
setwd(working.dir)
options(stringsAsFactors = saf.default)
rm(file, working.dir, saf.default,
envir=globalenv()) # This also seems awful?
stop(print(err.msg), call.=FALSE)
})
# Do more stuff, get more state, handle errors, then clean up.
# I.e can't use `finally` in previous tryCatch!
由此产生的错误消息仍然是很多丑陋的内部结构:
# <simpleError in file(file, "rt"): cannot open the connection>
# <simpleError: Could not open file 'somefile' with error message:
# Error in file(file, "rt"): cannot open the connection
>
# Error: Could not open file 'somefile' with error message:
# Error in file(file, "rt"): cannot open the connection
# In addition: Warning messages:
# 1: In file(file, "rt") :
# cannot open file 'somefile': No such file or directory
# 2: In stop(print(err.msg), call. = FALSE) :
# additional arguments ignored in stop()
>