October 2017
Beginner to intermediate
236 pages
7h 38m
English
Let’s write the function with the error handler implemented inside the body of the function. There are several ways to handle the exception, using the following steps:
logX <- function(numVec1){ if(any(numVec1<0)) stop("A negative value exists") else logV1 <- log(numVec1) return(logV1) }
logX1 <- function(numVec1){ try(logV1 <- log(numVec1)) return(logV1) }
logX2 <- function(numVec1){ tryCatch( return(logV1 <- log(numVec1)), warning = function(w) print("A negative input ...