Bob brings up a good point. Exceptions aren't part of the Stan language.
This conversation came up with a pull request that documented the C++ code for throwing std::out_of_range exceptions. In the past, we had only used std::domain_error.
From the language, in a sense, this isn't really like throwing an exception. It's an unrecoverable error at runtime. Here's a silly example:
parameters {
real alpha[1];
}
model {
alpha[2] ~ normal(0, 1);
}
Currently, this will show that the 100 tries for initialization failed. This is clearly a user error and no matter how many iterations of any algorithm that gets called will fix this.
Although it might seem like it gets tricker, I think this example should still stop as soon as the C++ throws a std::out_of_range exception:
parameters {
real alpha[1];
real<lower = 0, upper = 1> beta;
}
model {
if (beta > 0.7)
alpha[2] ~ normal(0, 1);
else
alpha[1] ~ normal(0, 1);
}
This will continue to sample and in this case, does the right thing for alpha and beta. (if the right thing is for beta to be uniform 0 to 0.7.)
The generated C++ and the log probability function faithfully codes this. I think it's the algorithm around it that should stop because there's something wrong with the evaluation of the log probability for those values and it's not just that the log probability evaluates to -inf at that point.
I'm not sure we need to have more fine-grained errors in the language except perhaps adding an exit() type of function. reject() will indicate some problem with that particular set of values and exit() should be reserved for when there's a problem so bad that you need to stop and think about what you're doing. Right now, we don't have anything in the language that will terminate the algorithm (any/all algorithm).
Daniel