[Rcpp-devel] calling R function triggers tryCatch() ???

Thomas Tse tommy_228_228 at yahoo.com.hk
Fri Oct 4 18:34:01 CEST 2013



Wow Romain!! Dirk and U are really 
"Professional R (and C++) Enthusiast" !!!

The following tests verified your words. I just post them for the 
benefits of other readers in this list.

With the following C++ functions 
exported:

// 
[[Rcpp::export]]
void 
testErr0(SEXP x, const Function & FUN)
{
  
Rcpp::Rcout << "before call" << std::endl;
  
FUN(x);
  
Rcpp::Rcout << "after call" << std::endl;
  
return;
}

// 
[[Rcpp::export]]
void 
testErr1(SEXP x, const Function & FUN)
{
  
Rcpp::Rcout << "before call" << std::endl;
  
Language call(FUN, x) ;
  
call.fast_eval();
  
Rcpp::Rcout << "after call" << std::endl;
  
return;
}

// 
[[Rcpp::export]]
void 
testErr2(SEXP x, const Function & FUN)
{
  
Rcpp::Rcout << "before call" << std::endl;
  
Language call(FUN, R_NilValue);
  
Language::Proxy proxy(call, 1);
  
proxy = x;
  
call.fast_eval();
  
Rcpp::Rcout << "after call" << std::endl;
  
return;
}

I tested the error handling behaviors in 
R: (AnyError() is R my implementation of a simplifed tryCatch(). It returns TRUE 
or FALSE)

> fff <- function(x) {x + 
1};
> testErr0(1:3, fff);
before call
after call
> testErr1(1:3, fff);
before call
after call
> testErr2(1:3, fff);
before call
after call
> fff(letters[1:3]);

Error in x + 1 : non-numeric argument to 
binary operator
> testErr0(letters, fff);
before call
Error: non-numeric argument to binary 
operator
> testErr1(letters, fff);
before call
Error in x + 1 : non-numeric argument to 
binary operator
> testErr2(letters, fff);
before call
Error in x + 1 : non-numeric argument to 
binary operator
> 
AnyError(fff(letters[1:3]));
[1] TRUE
> AnyError(testErr0(letters, fff));     
# !!! NO error throws by directly calling FUN
before call
[1] FALSE
> AnyError(testErr1(letters, 
fff));
before call
[1] TRUE
> AnyError(testErr2(letters, 
fff));
before call
[1] TRUE
> debug(tryCatch);
> fff(letters[1:3]);
Error in x + 1 : non-numeric argument to 
binary operator
> testErr1(letters, fff);
before call
Error in x + 1 : non-numeric argument to 
binary operator
> testErr2(letters, fff);
before call
Error in x + 1 : non-numeric argument to 
binary operator
> testErr0(letters, fff);
before call
debugging in: tryCatch(evalq(function 
(x)
{
    x + 1
}(c("a", "b", "c", "d", "e", "f", "g", 
"h", "i", "j", "k", "l",
"m", "n", "o", "p", "q", "r", "s", "t", 
"u", "v", "w", "x", "y",
"z")), <environment>), error = 
.rcpp_error_recorder)
...

the key points are:
1. tryCatch() is NOT triggered using the 
methods provided by Romain.
2. errors are not thrown if we directly 
call a Function FUN. (Handled nicely by Rcpp in the expense of 
speed)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20131005/71c597e1/attachment.html>


More information about the Rcpp-devel mailing list