So, I am trying to understand scope and functionality of tryCatch in R.
the following line:
arima(rep(1,3), order = c(1,0,0))
generates both warning and error, however in tryCatch block only warning function returns value. How can I get access to return value of both warning and error?
tryTest = tryCatch(
{
arima(rep(1,3), order = c(1,0,0))
},
warning = function(w) {
print('this is warning')
print(w)
return('return string from warning')
},
error = function(e) {
print('this is error')
print(e)
return('return string from error')
},
finally = {}
)
print(tryTest)
produces only:
"return string from warning"
tryCatch in R allows you to assign a value to the variable on error. Here are two minimal examples:
Similarly, you can return a value on warning as you already know. You should not write your tryCatch statement such that it could encounter both error and warning at the same time. I am not even sure if that is possible.
Edit: For completeness, I am adding an example with warning: