How to write try catch in R? A try-catch function allows the users to handle errors in the code and run effectively. The try-catch function checks whether a code contains any statements that can give an "error" or a "warning" message after execution and handles it. The trycatch () returns a value unless there is an error or a warning present. The try-catch () is necessary when there are user-defined inputs. This recipe performs an example of try-catch ()
a <- 5 b <- 'hi' c <- a+b print(c)
"Output of the line" : Error in a + b: non-numeric argument to binary operator Traceback:
tryCatch(("print the addition of two vectos" (c)), error = function(err) print("A numeric and character cannot be added"))
"Output of the code is" : "A numeric and character cannot be added"