Use tryCatch skip to next value of loop upon error?

后端 未结 5 1352
滥情空心
滥情空心 2020-11-29 15:51

I\'ve read a few other SO questions about tryCatch and cuzzins, as well as the documentation:

  • Exception handling in R
  • catching an error a
5条回答
  •  温柔的废话
    2020-11-29 16:09

    rm(list=ls())
    for (i in -3:3) {
      #ERROR HANDLING
      possibleError <- tryCatch({
        print(paste("Start Loop ", i ,sep=""))
        if(i==0){
          stop()
        }
      }
        ,
        error=function(e) {
          e
          print(paste("Oops! --> Error in Loop ",i,sep = ""))
        }
      )
    
      if(inherits(possibleError, "error")) next
    
      print(paste("  End Loop ",i,sep = ""))
    
    }
    

提交回复
热议问题