title: Can a function smash through tryCatch and friends?
body: <p>I know how to raise an error:</p>
  
  <pre><code>check_negative &lt;- function(x) {
    if (x &gt; 0) {
      stop("No way can x be positive.")
    }
    x
  }
  
  check_negative(5)
  #&gt; Error in check_negative(5) : No way can x be positive.
  </code></pre>
  
  <p>Since the purpose of <code>tryCatch</code> is to handle such errors, but <code>tryCatch</code> is not strictly reserved, I wondered if there is a way to always raise an error when <code>check_negative(5)</code> occurs, even if it's wrapped in <code>tryCatch</code>. </p>
  
  <p>My first approach works reasonably well, but needs to run at least once and doesn't fix <code>tryCatch</code> on exit. I tried putting the <code>tryCatch</code> 'repair' before the error within <code>on.exit</code>, but that resulted in the error not being raised.</p>
  
  <p>(Naturally, the following function does some damage to your R session, so run with care and don't run in a session you want preserved.)</p>
  
  <pre><code>check_negative &lt;- function(x) {
    unlockBinding("tryCatch", baseenv())
    assign("tryCatch", 
           local({
             function(expr, ..., finally) {
               expr
             }
           }), 
           pos = baseenv())
  
    if (x &gt; 0) {
      stop("No way can x be positive.")
    }
    # Attempt to repair tryCatch
    unlockBinding("tryCatch", baseenv())
    assign("tryCatch", 
           function (expr, ..., finally) 
           {
             tryCatchList &lt;- function(expr, names, parentenv, handlers) {
               nh &lt;- length(names)
               if (nh &gt; 1L) 
                 tryCatchOne(tryCatchList(expr, names[-nh], parentenv, 
                                          handlers[-nh]), names[nh], parentenv, handlers[[nh]])
               else if (nh == 1L) 
                 tryCatchOne(expr, names, parentenv, handlers[[1L]])
               else expr
             }
             tryCatchOne &lt;- function(expr, name, parentenv, handler) {
               doTryCatch &lt;- function(expr, name, parentenv, handler) {
                 .Internal(.addCondHands(name, list(handler), parentenv, 
                                         environment(), FALSE))
                 expr
               }
               value &lt;- doTryCatch(return(expr), name, parentenv, handler)
               if (is.null(value[[1L]])) {
                 msg &lt;- .Internal(geterrmessage())
                 call &lt;- value[[2L]]
                 cond &lt;- simpleError(msg, call)
               }
               else cond &lt;- value[[1L]]
               value[[3L]](cond)
             }
             if (!missing(finally)) 
               on.exit(finally)
             handlers &lt;- list(...)
             classes &lt;- names(handlers)
             parentenv &lt;- parent.frame()
             if (length(classes) != length(handlers)) 
               stop("bad handler specification")
             tryCatchList(expr, classes, parentenv, handlers)
           }, 
           pos = baseenv())
  }
  
  tryCatch(check_negative(5), error = function(e) NULL)
  #&gt; NULL
  tryCatch(check_negative(5), error = function(e) NULL)
  #&gt; Error in check_negative(5) : No way can x be positive.
  </code></pre>
  
  <p>My main motivation behind this question is curiosity; I don't intend to do this. But I do want to know whether something like this is possible or whether is is possible to write a function where an unsuspicious yet determined user would be unable to avoid an error.</p>