3 回答

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超3個(gè)贊
除了Shane的答案可以將您引向其他StackOverflow討論之外,您還可以嘗試使用代碼搜索功能。此原始答案指向Google的代碼搜索,此后已停止使用,但您可以嘗試
Github搜索,例如在此查詢中以language = R 搜索tryCatch;
Ohloh / Blackduck代碼搜索,例如此查詢R文件中的tryCatch
在整個(gè)Debian檔案庫中的Debian代碼搜索引擎
只是為了記錄在案,也有try,但tryCatch可能是可取的。我在Google代碼搜索中嘗試了一下快速計(jì)數(shù),但是嘗試為該動(dòng)詞本身獲取了太多的誤報(bào)-但它似乎tryCatch得到了更廣泛的使用。

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊
基本上,您想使用該tryCatch()功能。查看help(“ tryCatch”)了解更多詳細(xì)信息。
這是一個(gè)簡(jiǎn)單的示例(請(qǐng)記住,您可以執(zhí)行任何您想做的錯(cuò)誤操作):
vari <- 1
tryCatch(print("passes"), error = function(e) print(vari), finally=print("finished"))
tryCatch(stop("fails"), error = function(e) print(vari), finally=print("finished"))

TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個(gè)贊
該功能trycatch()相當(dāng)簡(jiǎn)單,并且有很多很好的教程。在Hadley Wickham的書Advanced-R中可以找到R中錯(cuò)誤處理的出色解釋,其后的內(nèi)容是一個(gè)非?;镜慕榻B,withCallingHandlers()并且withRestarts()用盡可能少的詞:
假設(shè)低級(jí)程序員編寫了一個(gè)函數(shù)來計(jì)算絕對(duì)值。他不確定如何計(jì)算,但知道如何構(gòu)造錯(cuò)誤并努力傳達(dá)自己的天真:
low_level_ABS <- function(x){
if(x<0){
#construct an error
negative_value_error <- structure(
# with class `negative_value`
class = c("negative_value","error", "condition"),
list(message = "Not Sure what to with a negative value",
call = sys.call(),
# and include the offending parameter in the error object
x=x))
# raise the error
stop(negative_value_error)
}
cat("Returning from low_level_ABS()\n")
return(x)
}
一個(gè)中級(jí)程序員還編寫了一個(gè)函數(shù),使用可悲的不完整low_level_ABS函數(shù)來計(jì)算絕對(duì)值。他知道,negative_value 當(dāng)?shù)闹禐閤負(fù)時(shí),低級(jí)代碼將引發(fā)錯(cuò)誤,并通過建立允許用戶控制錯(cuò)誤恢復(fù)(或不恢復(fù))的方式來建議解決問題的方法。restartmid_level_ABSmid_level_ABSnegative_value
mid_level_ABS <- function(y){
abs_y <- withRestarts(low_level_ABS(y),
# establish a restart called 'negative_value'
# which returns the negative of it's argument
negative_value_restart=function(z){-z})
cat("Returning from mid_level_ABS()\n")
return(abs_y)
}
最終,高級(jí)程序員使用該mid_level_ABS函數(shù)來計(jì)算絕對(duì)值,并建立條件處理程序,該條件處理程序通過使用重新啟動(dòng)處理程序來告訴 錯(cuò)誤mid_level_ABS從negative_value錯(cuò)誤中恢復(fù)。
high_level_ABS <- function(z){
abs_z <- withCallingHandlers(
# call this function
mid_level_ABS(z) ,
# and if an `error` occurres
error = function(err){
# and the `error` is a `negative_value` error
if(inherits(err,"negative_value")){
# invoke the restart called 'negative_value_restart'
invokeRestart('negative_value_restart',
# and invoke it with this parameter
err$x)
}else{
# otherwise re-raise the error
stop(err)
}
})
cat("Returning from high_level_ABS()\n")
return(abs_z)
}
所有這些的要點(diǎn)是,通過使用withRestarts()and withCallingHandlers(),該函數(shù) high_level_ABS能夠告訴您mid_level_ABS如何從錯(cuò)誤引起的low_level_ABS錯(cuò)誤中恢復(fù)而不會(huì)停止執(zhí)行 mid_level_ABS,這是您無法做到的tryCatch():
> high_level_ABS(3)
Returning from low_level_ABS()
Returning from mid_level_ABS()
Returning from high_level_ABS()
[1] 3
> high_level_ABS(-3)
Returning from mid_level_ABS()
Returning from high_level_ABS()
[1] 3
在實(shí)踐中,low_level_ABS代表一個(gè)mid_level_ABS調(diào)用很多(甚至可能數(shù)百萬次)的函數(shù),針對(duì)錯(cuò)誤的正確處理方法可能會(huì)因情況而異,如何處理特定錯(cuò)誤的選擇留給了更高級(jí)別的函數(shù)(high_level_ABS)。
- 3 回答
- 0 關(guān)注
- 806 瀏覽
添加回答
舉報(bào)