Skip to content

REprintf could be caught by tryCatch(message)

5 messages · Jan Gorecki, Luke Tierney

#
Dear R-devel community,

There appears to be an inconsistency in R C API about the exceptions
that can be raised from C code.
Mapping of R C funs to corresponding R functions is as follows.

error    -> stop
warning  -> warning
REprintf -> message
Rprintf  -> cat

Rprint/cat is of course not an exception, I listed it just for completeness.
The inconsistency I would like to report is about REprintf. It cannot
be caught by tryCatch(message). Warnings are errors are being caught
as expected.

Is there any chance to "fix"/"improve" REprintf so tryCatch(message)
can catch it?
So in the example below catch(Cmessage()) would behave consistently to
R's catch(message("a"))?

Regards,
Jan Gorecki

catch = function(expr) {
  tryCatch(expr,
    message=function(m) cat("caught message\n"),
    warning=function(w) cat("caught warning\n"),
    error=function(e) cat("caught error\n")
  )
}
library(inline)
Cstop = cfunction(c(), 'error("%s\\n","a"); return R_NilValue;')
Cwarning = cfunction(c(), 'warning("%s\\n","a"); return R_NilValue;')
Cmessage = cfunction(c(), 'REprintf("%s\\n","a"); return R_NilValue;')

catch(stop("a"))
#caught error
catch(warning("a"))
#caught warning
catch(message("a"))
#caught message

catch(Cstop())
#caught error
catch(Cwarning())
#caught warning
catch(Cmessage())
#a
#NULL
#
On Sun, 15 Sep 2019, Jan Gorecki wrote:

            
This is wrong: REpintf is like cat with file = stderr(). If this claim
is made somewhere in R documentation please report it a a bug.
No: this is behaving as intended.

Best,

luke

  
    
#
Thank you Luke for prompt reply.
Is it possible then to request a new function to R C API "message"
that would equivalent to R "message" function? Similarly as we now
have C "warning" and C "error" functions.

Best,
Jan
On Sun, Sep 15, 2019 at 5:25 PM Tierney, Luke <luke-tierney at uiowa.edu> wrote:
#
You can file it as a wishlist item in the bug trackign system. Without
a compelling case or a complete and well tested patch or both I doubt
it will rise to the top of anyone's priority list.

Best,

luke
On Sun, 15 Sep 2019, Jan Gorecki wrote:

            

  
    
#
Thank you Luke,
I filled https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17612

Best,
Jan
On Mon, Sep 16, 2019 at 2:15 AM Tierney, Luke <luke-tierney at uiowa.edu> wrote: