# When to throw

**URL:** <https://discourse.mc-stan.org/t/when-to-throw/337>\
**Category:** Developers\
**Created:** [April 12, 2017, 5:15pm UTC](https://discourse.mc-stan.org/t/when-to-throw/337 "2017-04-12T17:15:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sakrejda](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/sakrejda/32/845_2.png) [@sakrejda](https://discourse.mc-stan.org/u/sakrejda)\
**Post date:** [April 12, 2017, 5:15pm UTC](https://discourse.mc-stan.org/t/when-to-throw/337/1 "2017-04-12T17:15:01Z")

</div>

I have a higher-level test failing because grad\_reg\_inc\_gamma now throws on NaN, what should it do instead?

```
[RUN] AgradFwdGammaQ.nan
unknown file: Failure
C++ exception with description "grad_reg_inc_gamma: a is nan, but must not be nan!" thrown in the test body.
[FAILED] AgradFwdGammaQ.nan (1 ms)

```

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [April 12, 2017, 6:40pm UTC](https://discourse.mc-stan.org/t/when-to-throw/337/2 "2017-04-12T18:40:19Z")

</div>

Throwing is fine. You just want to test that it throws where it should.

```
EXPECT_THROW(statement, exception-type);

```

The exception type is something like `std::exception` or whatever the type is that’s being thrown here. We usually throw `std::domain_error` or `std::invalid_argument` for illegal arguments.

---

<div class="post-metadata">

**Author:** ![sakrejda](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/sakrejda/32/845_2.png) [@sakrejda](https://discourse.mc-stan.org/u/sakrejda)\
**Post date:** [April 12, 2017, 7:04pm UTC](https://discourse.mc-stan.org/t/when-to-throw/337/3 "2017-04-12T19:04:50Z")

</div>

these are auto-generated tests in `fwd` and `mix`, I’m just reverting to the old behavior for this function of immediately returning `nan` for `nan` arguments. Fixing this is a bigger project.
