Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The key to using <code>tryCatch</code> is realising that it returns an object. If there was an error inside the <code>tryCatch</code> then this object will inherit from class <code>error</code>. You can test for class inheritance with the function <code>inherit</code>.</p> <pre><code>x &lt;- tryCatch(stop("Error"), error = function(e) e) class(x) "simpleError" "error" "condition" </code></pre> <p><strong>Edit:</strong> </p> <p>What is the meaning of the argument <code>error = function(e) e</code>? 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 <code>tryCatch</code>ing. If an error is caught, it gets returned as the value of <code>tryCatch</code>. In the help documentation this is described as a <code>calling handler</code>. The argument <code>e</code> inside <code>error=function(e)</code> is the error message originating in your code.</p> <hr> <p>I come from the old school of procedural programming where using <code>next</code> was a bad thing. So I would rewrite your code something like this. (Note that I removed the <code>next</code> statement inside the <code>tryCatch</code>.):</p> <pre><code>for (i in 1:39487) { #ERROR HANDLING possibleError &lt;- tryCatch( thing(), error=function(e) e ) if(!inherits(possibleError, "error")){ #REAL WORK useful(i); fun(i); good(i); } } #end for </code></pre> <hr> <p>The function <code>next</code> is documented inside <code>?</code>for`.</p> <p>If you want to use that instead of having your main working routine inside an <code>if</code>, your code should look something like this:</p> <pre><code>for (i in 1:39487) { #ERROR HANDLING possibleError &lt;- tryCatch( thing(), error=function(e) e ) if(inherits(possibleError, "error")) next #REAL WORK useful(i); fun(i); good(i); } #end for </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload