Whenever you are creating a function, there could be a conditional statement or even some mathematical operation that might not be executable for all situations. For example, if you are transforming a numeric vector into logarithms, then a negative value will give an error. In this recipe, you will learn how to handle exceptions and errors and/or warnings while you write your own customized functions. There are several functions in R to handle exceptions/errors/warnings as follows:
- warning(): This function can generate a warning message
- stop(): This function can generate an error message
- supressWarnings(expr): This function evaluates the expression inside the function and then ignores warnings if there are any
- tryCatch(): This function (tryCatch() or try()) evaluates the code inside the parenthesis and then assigns an exception handler