Error Handling
Error Handling is a process in which we deal with unwanted or anomalous errors which may cause abnormal termination of the program during its execution. In R
- The stop() function will generate errors
- The stopifnot() function will take a logical expression and if any of the expressions is FALSE then it will generate the error specifying which expression is FALSE.
- The warning() will create the warning but will not stop the execution.
Error handling can be done using tryCatch(). The first argument of this function is the expression which is followed by the condition specifying how to handle the conditions.
Syntax:
check = tryCatch({ expression }, warning = function(w){ code that handles the warnings }, error = function(e){ code that handles the errors }, finally = function(f){ clean-up code })
Example:
R
# R program illustrating error handling # Evaluation of tryCatch check <- function (expression){ tryCatch (expression, warning = function (w){ message ( "warning:\n" , w) }, error = function (e){ message ( "error:\n" , e) }, finally = { message ( "Completed" ) }) } check ({10/2}) check ({10/0}) check ({10/ 'noe' }) |
Output:
Refer to the below articles to get detailed information about error handling in R
Learn R Programming
R is a Programming Language that is mostly used for machine learning, data analysis, and statistical computing. It is an interpreted language and is platform independent that means it can be used on platforms like Windows, Linux, and macOS.
In this R Language tutorial, we will Learn R Programming Language from scratch to advance and this tutorial is suitable for both beginners and experienced developers).