Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The only really detailed explanation I have seen can be found here: <a href="http://mazamascience.com/WorkingWithData/?p=912" rel="nofollow">http://mazamascience.com/WorkingWithData/?p=912</a></p> <p>Here is a code clip from that blog post showing how tryCatch works</p> <pre><code>#!/usr/bin/env Rscript # tryCatch.r -- experiments with tryCatch # Get any arguments arguments &lt;- commandArgs(trailingOnly=TRUE) a &lt;- arguments[1] # Define a division function that can issue warnings and errors myDivide &lt;- function(d, a) { if (a == 'warning') { return_value &lt;- 'myDivide warning result' warning("myDivide warning message") } else if (a == 'error') { return_value &lt;- 'myDivide error result' stop("myDivide error message") } else { return_value = d / as.numeric(a) } return(return_value) } # Evalute the desired series of expressions inside of tryCatch result &lt;- tryCatch({ b &lt;- 2 c &lt;- b^2 d &lt;- c+2 if (a == 'suppress-warnings') { e &lt;- suppressWarnings(myDivide(d,a)) } else { e &lt;- myDivide(d,a) # 6/a } f &lt;- e + 100 }, warning = function(war) { # warning handler picks up where error was generated print(paste("MY_WARNING: ",war)) b &lt;- "changing 'b' inside the warning handler has no effect" e &lt;- myDivide(d,0.1) # =60 f &lt;- e + 100 return(f) }, error = function(err) { # warning handler picks up where error was generated print(paste("MY_ERROR: ",err)) b &lt;- "changing 'b' inside the error handler has no effect" e &lt;- myDivide(d,0.01) # =600 f &lt;- e + 100 return(f) }, finally = { print(paste("a =",a)) print(paste("b =",b)) print(paste("c =",c)) print(paste("d =",d)) # NOTE: Finally is evaluated in the context of of the inital # NOTE: tryCatch block and 'e' will not exist if a warning # NOTE: or error occurred. #print(paste("e =",e)) }) # END tryCatch print(paste("result =",result)) </code></pre>
 

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