आप या तो
उपयोग कर सकते हैं:
#include <Rcpp.h>
RcppExport SEXP bar(SEXP x){
throw(Rcpp::exception("My Error Message","bar.cpp",4));
return x ;
}
जब मैं आर में इसे चलाने, यह मैं क्या मिलता है inline पैकेज जो try
/catch
आपके द्वारा जेनरेट किए गए फ़ंक्शन में ब्लॉक करता है (दो साधारण मैक्रोज़ का उपयोग करके)
या के रूप में अपने ब्लॉग पर उदाहरण के एक झुंड में दिखाया गया है इसे मैन्युअल रूप से अपने आप को करते हैं, या उदाहरण में/Rcpp पैकेज की धारा,
लेकिन क्या आप (यानी: बाहर एक कोशिश की फेंकने/पकड़ ब्लॉक) कभी काम नहीं कर सकता।
R> library(inline)
R> f <- cxxfunction(signature(), plugin="Rcpp", body='
+ throw std::range_error("boom");
+ return R_NilValue;
+ ')
R> f()
Error in f() : boom
R>
फिर, cxxfunction()
एक try
/catch()
ब्लॉक में यहाँ आप के लिए कहते हैं के रूप में आप अगर देख सकते हैं:
एक अतिरिक्त बोनस के रूप में, यहाँ पूर्ण उदाहरण (जो अनिवार्य रूप से पहले से ही इकाई परीक्षण में ही अस्तित्व में) है आप पर verbose
बारी:
R> f <- cxxfunction(signature(), plugin="Rcpp", body='
+ throw std::range_error("boom");
+ return R_NilValue;
+ ', verbose=TRUE)
>> setting environment variables:
PKG_LIBS = -L/usr/local/lib/R/site-library/Rcpp/lib \
-lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib
>> LinkingTo : Rcpp
CLINK_CPPFLAGS = -I"/usr/local/lib/R/site-library/Rcpp/include"
>> Program source :
1 :
2 : // includes from the plugin
3 :
4 : #include <Rcpp.h>
5 :
6 :
7 : #ifndef BEGIN_RCPP
8 : #define BEGIN_RCPP
9 : #endif
10 :
11 : #ifndef END_RCPP
12 : #define END_RCPP
13 : #endif
14 :
15 : using namespace Rcpp;
16 :
17 :
18 :
19 : // user includes
20 :
21 :
22 : // declarations
23 : extern "C" {
24 : SEXP file4cc53282() ;
25 : }
26 :
27 : // definition
28 :
29 : SEXP file4cc53282(){
30 : BEGIN_RCPP
31 :
32 : throw std::range_error("boom");
33 : return R_NilValue;
34 :
35 : END_RCPP
36 : }
37 :
38 :
Compilation argument:
/usr/lib/R/bin/R CMD SHLIB file4cc53282.cpp 2> file4cc53282.cpp.err.txt
ccache g++-4.6 -I/usr/share/R/include \
-I"/usr/local/lib/R/site-library/Rcpp/include" \
-fpic -g0 -O3 -Wall -pipe -Wno-unused -pedantic -c file4cc53282.cpp \
-o file4cc53282.o
g++ -shared -o file4cc53282.so file4cc53282.o \
-L/usr/local/lib/R/site-library/Rcpp/lib \
-lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib \
-L/usr/lib/R/lib -lR
R>
BEGIN_RCPP
और END_CPP
जादू तुम यहाँ की जरूरत जोड़ें।
कृपया करें अपने प्रश्न आरसीपीपी-डेवेल पर ले जाएं।
स्रोत
2011-12-08 22:48:19
धन्यवाद! उदाहरणों से, क्या आपका मतलब है 'copyMessageToR' और' Rf_error' जैसा कि RcppDateExample.cpp में उपयोग किया गया है? मुझे 'copyMessageToR' के लिए दस्तावेज़ नहीं मिला। – highBandWidth
आह ... लेकिन यहां पूछना ठीक काम करता है, मिनटों के भीतर उत्कृष्ट उत्तरों का उत्पादन, सब के बाद! मेरे अभिप्राय को समझें? ;) –
हां। 'कैच()' ब्लॉक में आप यही उपयोग करते हैं। –