stan-dev / stan

Stan development repository. The master branch contains the current release. The develop branch contains the latest stable development. See the Developer Process Wiki for details.
https://mc-stan.org
BSD 3-Clause "New" or "Revised" License
2.55k stars 365 forks source link

Remove places where exceptions are unconditionally swallowed. #3258

Closed WardBrian closed 4 months ago

WardBrian commented 5 months ago

Summary:

There are a few places where we catch std::exception and do not end up re-throwing it. This prevents features like https://github.com/stan-dev/stanc3/issues/1393 (an exit() function in the language), and leads to some unexpected behavior.

For example, if an indexing exception occurs during initialization, the program halts. If it occurs during sampling, it's all good. Run this program a few times to get a sense of this:

parameters {
  real mu;
}
model {
  mu ~ normal(0, 1);

  if (mu < 0) {
    array[1] int sigma;
    print(sigma[2]);
  }
}

Description:

I believe I have found the relevant places that need to change and highlighted them here: https://gist.github.com/WardBrian/a21b6e650606e562ca1466eedaa3eb47

When a similar change was made in https://github.com/stan-dev/stan/pull/2307, the choice was to not catch anything that isn't a std::domain_error. This is what reject() throws in the language, and a lot of our math functions as well, and generally seems like a good choice for "recoverable".

Current Version:

v2.34