22 #ifndef Rcpp__exceptions__h
23 #define Rcpp__exceptions__h
27 #ifndef RCPP_DEFAULT_INCLUDE_CALL
28 #define RCPP_DEFAULT_INCLUDE_CALL true
31 #define GET_STACKTRACE() R_NilValue
52 virtual const char*
what()
const throw() {
65 std::ostringstream ostr;
73 message(std::string(
"no such environment: '") + name +
"'") {};
77 virtual const char*
what()
const throw() {
return message.c_str(); };
91 virtual const char*
what()
const throw() {
return message.c_str(); };
114 ::Rf_warning(
"%s",
message.c_str());
124 namespace Rcpp {
namespace internal {
127 SEXP sentinel = PROTECT(Rf_allocVector(VECSXP, 1));
128 SET_VECTOR_ELT(sentinel, 0, token);
130 SEXP sentinelClass = PROTECT(Rf_mkString(
"Rcpp:longjumpSentinel"));
131 Rf_setAttrib(sentinel, R_ClassSymbol, sentinelClass) ;
139 Rf_inherits(x,
"Rcpp:longjumpSentinel") &&
140 TYPEOF(x) == VECSXP &&
145 return VECTOR_ELT(sentinel, 0);
152 ::R_ReleaseObject(token);
153 #if (defined(R_VERSION) && R_VERSION >= R_Version(3, 5, 0))
154 ::R_ContinueUnwind(token);
156 Rf_error(
"Internal error: Rcpp longjump failed to resume");
179 #if __cplusplus >= 201103L
187 #define RCPP_EXCEPTION_CLASS(__CLASS__,__WHAT__) \
188 class __CLASS__ : public std::exception{ \
190 __CLASS__( ) throw() : message( std::string(__WHAT__) + "." ){} ; \
191 __CLASS__( const std::string& message ) throw() : \
192 message( std::string(__WHAT__) + ": " + message + "." ){} ; \
193 virtual ~__CLASS__() throw(){} ; \
194 virtual const char* what() const throw() { return message.c_str() ; } \
196 std::string message ; \
199 #define RCPP_SIMPLE_EXCEPTION_CLASS(__CLASS__,__MESSAGE__) \
200 class __CLASS__ : public std::exception{ \
202 __CLASS__() throw() {} ; \
203 virtual ~__CLASS__() throw(){} ; \
204 virtual const char* what() const throw() { return __MESSAGE__ ; } \
234 #undef RCPP_SIMPLE_EXCEPTION_CLASS
235 #undef RCPP_EXCEPTION_CLASS
236 #undef RCPP_ADVANCED_EXCEPTION_CLASS
241 inline SEXP
nth(SEXP s,
int n) {
242 return Rf_length(s) > n ? (n == 0 ? CAR(s) : CAR(Rf_nthcdr(s, n))) : R_NilValue;
249 SEXP sys_calls_symbol = Rf_install(
"sys.calls");
250 SEXP identity_symbol = Rf_install(
"identity");
251 Shield<SEXP> identity_fun(Rf_findFun(identity_symbol, R_BaseEnv));
252 SEXP tryCatch_symbol = Rf_install(
"tryCatch");
253 SEXP evalq_symbol = Rf_install(
"evalq");
255 return TYPEOF(expr) == LANGSXP &&
256 Rf_length(expr) == 4 &&
257 nth(expr, 0) == tryCatch_symbol &&
258 CAR(
nth(expr, 1)) == evalq_symbol &&
259 CAR(
nth(
nth(expr, 1), 1)) == sys_calls_symbol &&
260 nth(
nth(expr, 1), 2) == R_GlobalEnv &&
261 nth(expr, 2) == identity_fun &&
262 nth(expr, 3) == identity_fun;
269 SEXP sys_calls_symbol = Rf_install(
"sys.calls");
276 while(CDR(cur) != R_NilValue) {
277 SEXP expr = CAR(cur);
291 #ifndef RCPP_USING_UTF8_ERROR_STRING
292 SET_STRING_ELT( res, 0, Rf_mkChar( ex_class.c_str() ) ) ;
294 SET_STRING_ELT( res, 0, Rf_mkCharLenCE( ex_class.c_str(), ex_class.size(), CE_UTF8 ) );
296 SET_STRING_ELT( res, 1, Rf_mkChar(
"C++Error" ) ) ;
297 SET_STRING_ELT( res, 2, Rf_mkChar(
"error" ) ) ;
298 SET_STRING_ELT( res, 3, Rf_mkChar(
"condition" ) ) ;
302 inline SEXP
make_condition(
const std::string& ex_msg, SEXP call, SEXP cppstack, SEXP classes){
304 #ifndef RCPP_USING_UTF8_ERROR_STRING
305 SET_VECTOR_ELT( res, 0, Rf_mkString( ex_msg.c_str() ) ) ;
308 SET_STRING_ELT( ex_msg_rstring, 0, Rf_mkCharLenCE( ex_msg.c_str(), ex_msg.size(), CE_UTF8 ) );
309 SET_VECTOR_ELT( res, 0, ex_msg_rstring ) ;
311 SET_VECTOR_ELT( res, 1, call ) ;
312 SET_VECTOR_ELT( res, 2, cppstack ) ;
315 SET_STRING_ELT( names, 0, Rf_mkChar(
"message" ) ) ;
316 SET_STRING_ELT( names, 1, Rf_mkChar(
"call" ) ) ;
317 SET_STRING_ELT( names, 2, Rf_mkChar(
"cppstack" ) ) ;
318 Rf_setAttrib( res, R_NamesSymbol, names ) ;
319 Rf_setAttrib( res, R_ClassSymbol, classes ) ;
323 template <
typename Exception>
326 std::string ex_class =
demangle(
typeid(ex).name() ) ;
328 std::string ex_class =
"<not available>";
330 std::string ex_msg = ex.what() ;
339 cppstack = R_NilValue;
342 SEXP condition = shelter(
make_condition( ex_msg, call, cppstack, classes) );
357 using namespace Rcpp;
359 #ifndef RCPP_USING_UTF8_ERROR_STRING
365 SET_STRING_ELT( tryError, 0, Rf_mkCharLenCE( str.c_str(), str.size(), CE_UTF8 ) );
366 Rcpp::Shield<SEXP> simpleErrorExpr( Rf_lang2(::Rf_install(
"simpleError"), tryError ));
370 Rf_setAttrib( tryError, R_ClassSymbol, Rf_mkString(
"try-error") ) ;
371 Rf_setAttrib( tryError, Rf_install(
"condition") , simpleError ) ;
380 std::string
demangle(
const std::string& name) ;
382 #define DEMANGLE(__TYPE__) demangle( typeid(__TYPE__).name() ).c_str()
387 SEXP stop_sym = Rf_install(
"stop" ) ;
390 Rf_eval( expr, R_GlobalEnv ) ;
394 SEXP stop_sym = Rf_install(
"stop" ) ;
397 Rf_eval( expr, R_GlobalEnv ) ;
self & field(const char *name_, T Class::*ptr, const char *docstring=0)
std::vector< std::string > stack
void copy_stack_trace_to_r() const
exception(const char *message_, bool include_call=RCPP_DEFAULT_INCLUDE_CALL)
bool include_call() const
void record_stack_trace()
exception(const char *message_, const char *, int, bool include_call=RCPP_DEFAULT_INCLUDE_CALL)
virtual const char * what() const
file_exists(const std::string &file)
virtual const char * what() const
std::string filePath() const
file_io_error(const std::string &msg, const std::string &file)
file_io_error(int code, const std::string &file)
file_io_error(const std::string &file)
file_not_found(const std::string &file)
virtual const char * what() const
no_such_env(const std::string &name)
#define RCPP_ADVANCED_EXCEPTION_CLASS(__CLASS__, __WHAT__)
void forward_rcpp_exception_to_r(const Rcpp::exception &ex)
void forward_exception_to_r(const std::exception &ex)
SEXP exception_to_try_error(const std::exception &ex)
std::string demangle(const std::string &name)
SEXP string_to_try_error(const std::string &str)
SEXP get_exception_classes(const std::string &ex_class)
SEXP rcpp_exception_to_r_condition(const Rcpp::exception &ex)
SEXP make_condition(const std::string &ex_msg, SEXP call, SEXP cppstack, SEXP classes)
SEXP exception_to_r_condition(const std::exception &ex)
SEXP exception_to_condition_template(const Exception &ex, bool include_call)
#define RCPP_DEFAULT_INCLUDE_CALL
bool isLongjumpSentinel(SEXP x)
SEXP getLongjumpToken(SEXP sentinel)
void resumeJump(SEXP token)
bool is_Rcpp_eval_call(SEXP expr)
SEXP longjumpSentinel(SEXP token)
Promise_Impl< PreserveStorage > Promise
Function_Impl< PreserveStorage > Function
RCPP_SIMPLE_EXCEPTION_CLASS(not_a_matrix, "Not a matrix.") RCPP_SIMPLE_EXCEPTION_CLASS(no_such_field
void warning(const char *fmt, Args &&... args)
LogicalVector in(const VectorBase< RTYPE, NA, T > &x, const VectorBase< RTYPE, RHS_NA, RHS_T > &table)
SEXP Rcpp_fast_eval(SEXP expr, SEXP env)
static std::string toString(const int i)
void NORET stop(const char *fmt, Args &&... args)
No such field RCPP_EXCEPTION_CLASS(reference_creation_error, "Error creating object of reference class") RCPP_ADVANCED_EXCEPTION_CLASS(not_compatible
S4_Impl< PreserveStorage > S4
attribute_hidden SEXP rcpp_set_stack_trace(SEXP e)
attribute_hidden SEXP rcpp_get_stack_trace()
LongjumpException(SEXP token_)