# Best way to enforce argument requirements (assert vs. exception, or something else)?

**URL:** <https://discourse.julialang.org/t/best-way-to-enforce-argument-requirements-assert-vs-exception-or-something-else/6293>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 6, 2017, 4:01pm UTC](https://discourse.julialang.org/t/best-way-to-enforce-argument-requirements-assert-vs-exception-or-something-else/6293 "2017-10-06T16:01:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 6, 2017, 4:01pm UTC](https://discourse.julialang.org/t/best-way-to-enforce-argument-requirements-assert-vs-exception-or-something-else/6293/1 "2017-10-06T16:01:32Z")

</div>

I have a function that expects a positive number as argument, for example:

```julia
f(x) = sqrt(x)

```

I want to be safe about this, so I want to throw an error if x \< 0. Currently I do an assert:

```julia
function f(x)
  @assert x >= 0
  sqrt(x)
end

```

Is this the recommended way? Or should I throw an error instead? Are there guidelines for this?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 6, 2017, 4:19pm UTC](https://discourse.julialang.org/t/best-way-to-enforce-argument-requirements-assert-vs-exception-or-something-else/6293/2 "2017-10-06T16:19:01Z")

</div>

> [@e3c6](#):
>
> Is this the recommended way?

No.

> [@e3c6](#):
>
> Or should I throw an error instead?

Yes, but not an `error` most of the time.

> [@e3c6](#):
>
> Are there guidelines for this?

[https://docs.julialang.org/en/v1/manual/control-flow/#Built-in-Exceptions](https://docs.julialang.org/en/v1/manual/control-flow/#Built-in-Exceptions) and the multiple entries following [https://docs.julialang.org/en/v1/base/base/#Core.ArgumentError](https://docs.julialang.org/en/v1/base/base/#Core.ArgumentError) tells you when to use which excepiton type.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 6, 2017, 6:49pm UTC](https://discourse.julialang.org/t/best-way-to-enforce-argument-requirements-assert-vs-exception-or-something-else/6293/3 "2017-10-06T18:49:10Z")

</div>

Also see

> **[GitHub - jw3126/ArgCheck.jl: Package for checking function arguments](https://github.com/jw3126/ArgCheck.jl)**
>
> Package for checking function arguments. Contribute to jw3126/ArgCheck.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![ptoche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptoche/32/23554_2.png) [@ptoche](https://discourse.julialang.org/u/ptoche)\
**Post date:** [April 23, 2021, 11:47pm UTC](https://discourse.julialang.org/t/best-way-to-enforce-argument-requirements-assert-vs-exception-or-something-else/6293/4 "2021-04-23T23:47:29Z")

</div>

This comes up high in google searches. It’s a pretty good FAQ.

Reference links to the manual are dead. I think these are the new ones:

[https://docs.julialang.org/en/v1/base/base/#Errors](https://docs.julialang.org/en/v1/base/base/#Errors)

[https://docs.julialang.org/en/v1/manual/control-flow/](https://docs.julialang.org/en/v1/manual/control-flow/)
