How to write trycatch in R

前端 未结 5 2222
灰色年华
灰色年华 2020-11-21 23:03

I want to write trycatch code to deal with error in downloading from the web.

url <- c(
    \"http://stat.ethz.ch/R-manual/R-devel/library/ba         


        
5条回答
  •  臣服心动
    2020-11-21 23:19

    R uses functions for implementing try-catch block:

    The syntax somewhat looks like this:

    result = tryCatch({
        expr
    }, warning = function(warning_condition) {
        warning-handler-code
    }, error = function(error_condition) {
        error-handler-code
    }, finally={
        cleanup-code
    })
    

    In tryCatch() there are two ‘conditions’ that can be handled: ‘warnings’ and ‘errors’. The important thing to understand when writing each block of code is the state of execution and the scope. @source

提交回复
热议问题