# When to return NaN vs throw an Error?

**URL:** <https://discourse.julialang.org/t/when-to-return-nan-vs-throw-an-error/89587>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 1, 2022, 1:42am UTC](https://discourse.julialang.org/t/when-to-return-nan-vs-throw-an-error/89587 "2022-11-01T01:42:36Z")\
**Posts on this page:** 1\
**Showing post:** 16

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [November 2, 2022, 2:53am UTC](https://discourse.julialang.org/t/when-to-return-nan-vs-throw-an-error/89587/16 "2022-11-02T02:53:04Z")

</div>

> [@Alec\_Loudenback](#):
>
> `positive = sum(x for x in v if x > 0)`

As a side note, this code will throw an error on Julia 1.3 or earlier if `v` does not contain any positive numbers. See this thread:

> [@Inconsistency in \`sum\` of an empty generator](https://discourse.julialang.org/t/inconsistency-in-sum-of-an-empty-generator/89638):
>
> On Julia 1.3, the following two empty generators both throw an error when you apply sum to them: julia\> g1 = (1 for i in Int[]); julia\> g2 = (x for x in [-1] if x \> 0); julia\> eltype(g1) Any julia\> eltype(g2) Any julia\> sum(g1) ERROR: ArgumentError: reducing over an empty collection is not allowed julia\> sum(g2) ERROR: ArgumentError: reducing over an empty collection is not allowed That seems like the correct behavior. On newer versions of Julia, you can avoid the error by setting the ini…

---

_[View the full topic](https://discourse.julialang.org/t/when-to-return-nan-vs-throw-an-error/89587)._
