I have added the try/catch in RStan a couple days ago.
During testing, I found a problem for reading data: testing the variable existence is done after
checking dims; so if a variable is missing, it would say:
> fit1b <- stan(fit = fit1, data = list(mu2 = 1), chains = 1) # variable mu missing
Error in new_CppObject_xp(fields$.module, fields$.pointer, ...) :
mismatch in number dimensions declared and found in context; dims declared=(1); dims found=()
This is an example code from which we can see the problem:
class model16726ca5dafd_c2 : public prob_grad_ad {
private:
vector<matrix_d> mu;
public:
model16726ca5dafd_c2(stan::io::var_context& context__,
std::ostream* pstream__ = 0)
: prob_grad_ad::prob_grad_ad(0) {
static const char* function__ = "model16726ca5dafd_c2_namespace::model16726ca5dafd_c2(%1%)";
(void) function__; // dummy call to supress warning
size_t pos__;
(void) pos__; // dummy call to supress warning
std::vector<int> vals_i__;
std::vector<double> vals_r__;
context__.validate_dims("mu", context__.to_vec(3,4,5));
mu = std::vector<matrix_d>(3,matrix_d(4,5));
if (!context__.contains_r("mu"))
throw std::runtime_error("variable mu not found.");
--
Jiqiang