Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use tryCatch skip to next value of loop upon error?

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

  • Exception handling in R
  • catching an error and then branching logic
  • How can I check whether a function call results in a warning?
  • Problems with Plots in Loop

but I still don't understand.

I'm running a loop and want to skip to next if any of a few kinds of errors occur:

for (i in 1:39487) {    # EXCEPTION HANDLING   this.could.go.wrong <- tryCatch(                            attemptsomething(),                            error=function(e) next                          )   so.could.this <- tryCatch(                      doesthisfail(),                      error=function(e) next                    )    catch.all.errors <- function() { this.could.go.wrong; so.could.this; }   catch.all.errors;     #REAL WORK   useful(i); fun(i); good(i);    }  #end for 

(by the way, there is no documentation for next that I can find)

When I run this, R honks:

Error in value[[3L]](cond) : no loop for break/next, jumping to top level 

What basic point am I missing here? The tryCatch's are clearly within the for loop, so why doesn't R know that?

like image 290
isomorphismes Avatar asked Nov 11 '11 12:11

isomorphismes


People also ask

How do I continue a loop if error in R?

One of the easier ways is to ignore them and continue moving through the loop. This is accomplished with the try function which simply wraps around the entire body of the loop. By default, try will continue the loop even if there's an error, but will still show the error message.

How do I skip an error message in R?

8.3 Ignoring conditions The simplest way of handling conditions in R is to simply ignore them: Ignore errors with try() . Ignore warnings with suppressWarnings() . Ignore messages with suppressMessages() .

How do you catch an error in R?

tryCatch() in R The tryCatch() function in R evaluates an expression with the possibility to catch exceptions. The class of the exception thrown by a standard stop() call is try-error. The tryCatch() function allows the users to handle errors. With it, you can do things like: if(error), then(do this).


1 Answers

The key to using tryCatch is realising that it returns an object. If there was an error inside the tryCatch then this object will inherit from class error. You can test for class inheritance with the function inherit.

x <- tryCatch(stop("Error"), error = function(e) e) class(x) "simpleError" "error"       "condition"   

Edit:

What is the meaning of the argument error = function(e) e? This baffled me, and I don't think it's well explained in the documentation. What happens is that this argument catches any error messages that originate in the expression that you are tryCatching. If an error is caught, it gets returned as the value of tryCatch. In the help documentation this is described as a calling handler. The argument e inside error=function(e) is the error message originating in your code.


I come from the old school of procedural programming where using next was a bad thing. So I would rewrite your code something like this. (Note that I removed the next statement inside the tryCatch.):

for (i in 1:39487) {   #ERROR HANDLING   possibleError <- tryCatch(       thing(),       error=function(e) e   )    if(!inherits(possibleError, "error")){     #REAL WORK     useful(i); fun(i); good(i);   }  }  #end for 

The function next is documented inside ?for`.

If you want to use that instead of having your main working routine inside an if, your code should look something like this:

for (i in 1:39487) {   #ERROR HANDLING   possibleError <- tryCatch(       thing(),       error=function(e) e   )    if(inherits(possibleError, "error")) next    #REAL WORK   useful(i); fun(i); good(i);  }  #end for 
like image 146
Andrie Avatar answered Sep 22 '22 18:09

Andrie