Rcpp Version 0.12.12
UncaughtExceptions.r
Go to the documentation of this file.
1 #!/usr/bin/env r
2 #
3 # Copyright (C) 2009 - 2010 Romain Francois and Dirk Eddelbuettel
4 #
5 # This file is part of Rcpp.
6 #
7 # Rcpp is free software: you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # Rcpp is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
19 
20 require(Rcpp)
21 
22 
23 ## NOTE: This is the old way to compile Rcpp code inline.
24 ## The code here has left as a historical artifact and tribute to the old way.
25 ## Please use the code under the "new" inline compilation section.
26 
27 require(inline)
28 funx_old <- cxxfunction(
29  signature(),
30  'throw std::range_error("boom"); return R_NilValue ; ',
31  plugin = "Rcpp" )
32 
33 ## NOTE: Within this section, the new way to compile Rcpp code inline has been
34 ## written. Please use the code next as a template for your own project.
35 
36 cppFunction('
37 SEXP funx(){
38  throw std::range_error("boom"); return R_NilValue ;
39 }')
40 
41 tryCatch( funx(), "C++Error" = function(e){
42  cat( sprintf( "C++ exception of class '%s' : %s\n", class(e)[1L], e$message ) )
43 } )
44 # or using a direct handler
45 tryCatch( funx(), "std::range_error" = function(e){
46  cat( sprintf( "C++ exception of class '%s' : %s\n", class(e)[1L], e$message ) )
47 } )
48 # just to check things carry on
49 print( rnorm(10) )
50