背景
在使用R进行循环操作时,出现错误,为了对错误进行分析,使用tryCatch()函数抓取处理错误
使用
tryCatch常用的三种用法:
抓取错误
tryCatch(libray(test), error = function(e){print("出现错误")} )
## [1] "出现错误"
释义:当加载test包时,出现错误,执行error函数
这里很适合在循环中出现错误的情况,可以对错误的循环结果进行赋值
抓取警告和错误
tryCatch(stop("test"), warning = function(w){print("出现警告")},finally={print("test")})## Error in doTryCatch(return(expr), name, parentenv, handler) : test## [1] "test"
释义:执行语句出错,打印出错误,并执行finally函数
如果单独只有warning函数,则只是打印错误
继续执行
tryCatch(a = 1, finally={print("我是个测试")})## Error in tryCatchList(expr, classes, parentenv, handlers) :## argument "expr" is missing, with no default## [1] "我是个测试"
finally函数无论错与对都会执行
封装函数
log_calculator <- function(x){tryCatch(expr = {message(log(x))message("Successfully executed the log(x) call.")},error = function(e){message('Caught an error!')print(e)},warning = function(w){message('Caught an warning!')print(w)},finally = {message('All done, quitting.')})}
测试结果如下:
- 如果
x是有效数字,则expr和finally被执行:
log_calculator(-10)
## Caught an warning!
## <simpleWarning in log(x): NaNs produced>
## All done, quitting.
- 如果
x不是有效数字,则expr会报错,warning和finally被执行:
log_calculator("10")
## Caught an error!
## <simpleError in log(x): non-numeric argument to mathematical function>
## All done, quitting.
