While you manually run warning, your expression also throws an error because you are using returnoutside the function.
This becomes more apparent if you return the error message itself in function(e)(instead of typing "error"):
tryCatch({
1+1
warning("test")
return(2)
}, error=function(e) {
e
})
# <simpleError in doTryCatch(return(expr), name, parentenv, handler):
# no function to return from, jumping to top level>
# Warning message:
# In doTryCatch(return(expr), name, parentenv, handler) : test
(Note that this is equivalent to throwing an argument error.)
, , return(2) R:
return(2)
# Error: no function to return from, jumping to top level
, return , :
tryCatch({
1+1
warning("test")
2
}, error=function(e){
print('error')
})