Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
5 views
in R Programming by (4k points)
recategorized by

How to write a trycatch code to sort out an error in web downloading.

    url <- c(
        "http://xyz.com/library/connections.html",
        "http://WWW.fb.com/Server/Xy")
    y <- mapply(readLines, con=url

This runs successfully.  

    url <- c("xxxxx", "http://en.wikipedia.org/wiki/Xz
    "

 The error shows is url[1] doesn't exist and I want to write a trycatch loop to solve it, so:

  • If any mistakes in URL then error : "web URL is wrong, can't get" showed.
  • If any mistakes in URL then code just skips it and download util the end of all URL lists.

1 Answer

0 votes
by (50.2k points)

Let me tell you that R basically executes the functions for implementing try-catch block:

The syntax of that function looks like the following code:

result = tryCatch({

    expr

}, warning = function(warning_condition) {

    warning-handler-code

}, error = function(error_condition) {

    error-handler-code

}, finally={

    cleanup-code

})

In tryCatch() there are in all two ‘conditions’ that can be handled. The conditions are ‘warnings’ and ‘errors’. Note that you have to write each block of code is the state of execution and the scope. If you are a beginner and want to know more about R then do check out the R programming tutorial.

Related questions

0 votes
1 answer
+1 vote
1 answer
+1 vote
1 answer
+1 vote
1 answer
+1 vote
1 answer

Browse Categories

...