Stats

14 Contributors: 1 Monday, August 8, 2016
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

Fault-tolerant/resilient code

Parameters

ParameterDetails
exprIn case the "try part" was completed successfully tryCatch will return the last evaluated expression. Hence, the actual value being returned in case everything went well and there is no condition (i.e. a warning or an error) is the return value of readLines. Note that you don't need to explicilty state the return value via return as code in the "try part" is not wrapped insided a function environment (unlike that for the condition handlers for warnings and error below)
warning/error/etcProvide/define a handler function for all the conditions that you want to handle explicitly. AFAIU, you can provide handlers for any type of conditions (not just warnings and errors, but also custom conditions; see simpleCondition and friends for that) as long as the name of the respective handler function matches the class of the respective condition (see the Details part of the doc for tryCatch).
finallyHere goes everything that should be executed at the very end, regardless if the expression in the "try part" succeeded or if there was any condition. 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> (i.e. the same logic as for "try part".

Remarks

tryCatch

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

Implications of choosing specific return values of the handler functions

As we've specified that NA should be returned in case of an error in the "try part", 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/drop" 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

When the third element of our urls vector hits our function, we get the following warning in addition to the fact that an error occurs (readLines first complains that it can't open the connection via a warning before actually failing with an error):

Warning message:
    In file(con, "r") : cannot open file 'I'm no URL': No such file or directory

An error "wins" over a warning, so we're not really interested in the warning in this particular case. Thus we have set warn = FALSE in readLines, but that doesn't seem to have any effect. An alternative way to suppress the warning is to use

suppressWarnings(readLines(con = url))

instead of

readLines(con = url, warn = FALSE)

Related Examples