Or even just call Rcpp::sourceCpp() on a C++ file like this
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int throw_exception() {
std::stringstream errmsg;
errmsg << "this is an exception";
throw std::domain_error(errmsg.str());
return 0;
}