轉載自:https://blog.csdn.net/luoyexuge/article/details/49105299
(1)抓取錯誤
tryCatch(libray(xx),error=function(e){print("出現錯誤")} )
當xx包出現錯誤時候就會執行error函數 ,把error的結果執行出來
(2)抓取錯誤和警告
tryCatch(libray(xx),warning = function(w){print("出現警告")},
error=function(e){print("出現錯誤")} )
(3)finally最后都會執行,跟python和java類似
tryCatch(libray(xx),warning = function(w){print("出現警告")},
error=function(e){print("出現錯誤")},
finally={
print("導入ggplot2包")
library(ggplot2)
}
)