Functions and Try() in R

Functions and try() in R

If you read closely error message you could see that odbcConnect gives you warning. Error is generated by ODBC drivers and it isn't error in try meaning (geterrmessage() won't work either).

You could use tryCatch to handle this, e.g.:

tryCatch(odbcConnect("TEST"), warning=function(w) print("FAIL!"))

Some more explanation:

-1 is a result of odbcDriverConnect function. If you look at the code there are lines

stat <- .Call(C_RODBCDriverConnect, as.character(connection), 
id, as.integer(believeNRows), as.logical(readOnlyOptimize))
if (stat < 0L) {
warning("ODBC connection failed")
return(stat)
}

So you end without errors (and with a warning) and with integer code from C-level. Actually this code is returned when connection is succeed too (but then is equal 1). When there is no errors then result class can't be try-error.

It is not problem with try and functions but specific of this particular function (odbcDriverConnect).

You could of course use this behaviour as in your example

ch <- odbcConnect("TEST")
if (ch != -1)

With tryCatch you could do

tryCatch(ch<-odbcConnect("TEST"), warning=function(w) print("FAIL!"))

which creates ch variable when succeed and print message when failed.

Or

ch <- tryCatch(odbcConnect("TEST"), warning=function(w) {print("FAIL!");return(NA)})

which always creates ch variable but in case of failure there is NA value.

How to write trycatch in R

Well then: welcome to the R world ;-)

Here you go

Setting up the code

urls <- c(
"http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html",
"http://en.wikipedia.org/wiki/Xz",
"xxxxx"
)
readUrl <- function(url) {
out <- tryCatch(
{
# Just to highlight: if you want to use more than one
# R expression in the "try" part then you'll have to
# use curly brackets.
# 'tryCatch()' will return the last evaluated expression
# in case the "try" part was completed successfully

message("This is the 'try' part")

readLines(con=url, warn=FALSE)
# The return value of `readLines()` is the actual value
# that will be returned in case there is no condition
# (e.g. warning or error).
# You don't need to state the return value via `return()` as code
# in the "try" part is not wrapped inside a function (unlike that
# for the condition handlers for warnings and error below)
},
error=function(cond) {
message(paste("URL does not seem to exist:", url))
message("Here's the original error message:")
message(cond)
# Choose a return value in case of error
return(NA)
},
warning=function(cond) {
message(paste("URL caused a warning:", url))
message("Here's the original warning message:")
message(cond)
# Choose a return value in case of warning
return(NULL)
},
finally={
# NOTE:
# Here goes everything that should be executed at the end,
# regardless of success or error.
# If you want more than one expression to be executed, then you
# need to wrap them in curly brackets ({...}); otherwise you could
# just have written 'finally=<expression>'
message(paste("Processed URL:", url))
message("Some other message at the end")
}
)
return(out)
}

Applying the code

> y <- lapply(urls, readUrl)
Processed URL: http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html
Some other message at the end
Processed URL: http://en.wikipedia.org/wiki/Xz
Some other message at the end
URL does not seem to exist: xxxxx
Here's the original error message:
cannot open the connection
Processed URL: xxxxx
Some other message at the end
Warning message:
In file(con, "r") : cannot open file 'xxxxx': No such file or directory

Investigating the output

> head(y[[1]])
[1] "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"
[2] "<html><head><title>R: Functions to Manipulate Connections</title>"
[3] "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"
[4] "<link rel=\"stylesheet\" type=\"text/css\" href=\"R.css\">"
[5] "</head><body>"
[6] ""

> length(y)
[1] 3

> y[[3]]
[1] NA

Additional remarks

tryCatch

tryCatch returns the value associated to executing expr unless there's an error or a warning. In this case, specific return values (see return(NA) above) can be specified by supplying a respective handler function (see arguments error and warning in ?tryCatch). These can be functions that already exist, but you can also define them within tryCatch() (as I did above).

The implications of choosing specific return values of the handler functions

As we've specified that NA should be returned in case of error, the third element in y is NA. If we'd have chosen NULL to be the return value, the length of y would just have been 2 instead of 3 as lapply() will simply "ignore" return values that are NULL. Also note that if you don't specify an explicit return value via return(), the handler functions will return NULL (i.e. in case of an error or a warning condition).

"Undesired" warning message

As warn=FALSE doesn't seem to have any effect, an alternative way to suppress the warning (which in this case isn't really of interest) is to use

suppressWarnings(readLines(con=url))

instead of

readLines(con=url, warn=FALSE)

Multiple expressions

Note that you can also place multiple expressions in the "actual expressions part" (argument expr of tryCatch()) if you wrap them in curly brackets (just like I illustrated in the finally part).

Try an expression multiple times until it succeeds in R

1) We could turn it into a function which returns x if it finds one or stops if not. Also we use for instead of while and if instead of ifelse.

retry <- function() {
for(i in 1:100) {
rand <- runif(1)
x <- if (rand > 0.3) rand else NA
if (!is.na(x)) return(x)
}
stop("x is NA")
}

retry()

2) or if you don't want the stop in the function then remove the stop line replacing it with a line that returns x and then use this (although it does involve testing x for NA twice):

x <- retry()
if (is.na(x)) stop("x is NA")

3) or another option is to pass the bad value to the function. Because of lazy evaluation the bad argument is only evaluated if it is, in fact, bad:

retry2 <- function(bad) {
for(i in 1:100) {
rand <- runif(1)
x <- if (rand > 0.3) rand else NA
if (!is.na(x)) return(x)
}
bad
}

retry2(stop("x is NA"))

4) If you don't mind testing x for NA twice using break also works even without a function:

for(i in 1:100) {
rand <- runif(1)
x <- if (rand > 0.3) rand else NA
if (!is.na(x)) break
}
if (is.na(x)) stop("x is NA")
x

How to assign in try-catch in R

You can use <<- assignment to access parent environment variables. In your case from error block, it will assign to the parent environment which contains vector_a. But not sure if it is a good practice.

error = function(e){
vector_a <<- c(vector_a, "NO")
}

delay a function if it throws an error: try_catch and if else

Here's a solution based on Roland's suggestion:

muhur <- function(x, tries = 2){
if (tries <= 0)
stop("Too many tries")
result <- try(read.csv(x))
if (inherits(result, "try-error")) {
cat("Failed, trying again in 10 seconds...\n")
Sys.sleep(10)
muhur(x, tries = tries - 1)
} else
result
}

I couldn't get a withRestarts solution to work; I find its documentation and blog writeups about it all pretty opaque.

How can I handle catch exception for different functions at a common class in Kotlin?

Your getResult function has an error in the catch block. You are calling call() again if the first call to call() threw an exception. I don't think it makes sense to call it again, because it will probably throw again and you won't be able to return your Resource.error.

If you fix this problem, you won't need to use try/catch in your other functions, because call will be safely wrapped in try/catch and pack any errors into a Resource.error.

To fix it: since you already handle response?.code in the try block, there's no reason to repeat getting call()?.code in the catch block. You can simplify it to

return error(e.message ?: e.toString(), AppConstants.INetworkValues.DEFAULT_ERROR_CODE)


Related Topics



Leave a reply



Submit