# Style guide for throwing errors?

**URL:** <https://discourse.julialang.org/t/style-guide-for-throwing-errors/17789>\
**Category:** General Usage\
**Tags:** error-message\
**Created:** [November 20, 2018, 7:39pm UTC](https://discourse.julialang.org/t/style-guide-for-throwing-errors/17789 "2018-11-20T19:39:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [November 20, 2018, 7:39pm UTC](https://discourse.julialang.org/t/style-guide-for-throwing-errors/17789/1 "2018-11-20T19:39:53Z")

</div>

Hi,

I have two questions about programming style and errors in Julia:

1. Is it considered good style to have functions validate their inputs, and throw informative errors if an input’s value would cause an error?
2. What type of error should be thrown in that case? I have been using `ArgumentError` but when I look at the documentation, it seems like `ArgumentError` may have been intended for situations where no method matching the type signature at the call site was found.

---

<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:** [November 20, 2018, 8:24pm UTC](https://discourse.julialang.org/t/style-guide-for-throwing-errors/17789/2 "2018-11-20T20:24:24Z")

</div>

> [@galenlynch](#):
>
> Is it considered good style to have functions validate their inputs, and throw informative errors if an input’s value would cause an error?

I think yes. But exploit also types. That is, instead of throwing an error if `x` is not of type `T`, define your function to take argument `x::T` explicitly.

> [@galenlynch](#):
>
> it seems like `ArgumentError` may have been intended for situations where no method matching the type signature at the call site was found

I think that’s `MethodError`.  
You might also take a look at `DomainError`, if your function is a math function.

---

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [November 20, 2018, 9:00pm UTC](https://discourse.julialang.org/t/style-guide-for-throwing-errors/17789/3 "2018-11-20T21:00:22Z")

</div>

So `ArgumentError` is an ok error to throw if, for example, a function requires an argument to be positive, but a negative value is received?

---

<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:** [November 20, 2018, 9:15pm UTC](https://discourse.julialang.org/t/style-guide-for-throwing-errors/17789/4 "2018-11-20T21:15:35Z")

</div>

I think so. Or `DomainError`. For example `sqrt(-2)` throws a `DomainError`, which is more appropriate if the mathematical definition of “domain” applies to your function.

You can see some examples of `ArgumentError` usage by the pros in the source of Julia itself

[https://github.com/JuliaLang/julia/search?q=ArgumentError&unscoped\_q=ArgumentError](https://github.com/JuliaLang/julia/search?q=ArgumentError&unscoped_q=ArgumentError)

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [November 20, 2018, 9:17pm UTC](https://discourse.julialang.org/t/style-guide-for-throwing-errors/17789/5 "2018-11-20T21:17:08Z")

</div>

I like ArgCheck.jl

---

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [November 20, 2018, 9:28pm UTC](https://discourse.julialang.org/t/style-guide-for-throwing-errors/17789/6 "2018-11-20T21:28:10Z")

</div>

Man, where has ArgCheck.jl been all my life? Thanks for the tip!

---

<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:** [November 20, 2018, 9:30pm UTC](https://discourse.julialang.org/t/style-guide-for-throwing-errors/17789/7 "2018-11-20T21:30:13Z")

</div>

I had forgotten about ArgCheck! It’s very nice.
