我有一个功能:
buggy <- function(...) {
tryCatch({
itWorked <- FALSE
stop("I don't like green eggs and ham!")
itWorked <- TRUE
}, finally = {
if ( itWorked )
return("I do, Sam I am")
else
return("I do not like them, Sam I am!")
})
}
基本上,buggy
尝试进行一些可能成功也可能不成功的计算(由 . 确定itWorked
。该finally
子句只是确保即使计算不起作用,也会返回一些东西(在这种情况下,"I do not like them, Sam I am!"
)。
它按预期工作:
> buggy()
Error in tryCatchList(expr, classes, parentenv, handlers) :
I don't like green eggs and ham!
[1] "I do not like them, Sam I am!"
现在我想听听以下错误buggy()
:
tryCatch( buggy(),
error=function(e) message('too bad! there was an error') )
然而,错误buggy
未能在周围引发错误tryCatch
:
> tryCatch( buggy(),
+ error=function(e) message('too bad! there was an error') )
[1] "I do not like them, Sam I am!"
我希望这会说:
'too bad! there was an error'
[1] "I do not like them, Sam I am!"
谁能告诉我为什么这不起作用?我是否需要从内部“提出”错误buggy
?