Handling exceptions and error messages
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 messagestop()
: This function can generate an error messagesupressWarnings(expr)
: This function evaluates the expression inside the function and then ignores warnings if there are anytryCatch()
: This function (tryCatch()
ortry()
) evaluates the code inside the parenthesis and then assigns an exception handler
In this recipe, you will learn how to use an exception handler by creating your function.
Getting ready
Suppose you are transforming...