# Does Julia have the Python equivalent of ValueError?

**URL:** <https://discourse.julialang.org/t/does-julia-have-the-python-equivalent-of-valueerror/62684>\
**Category:** General Usage\
**Tags:** exception\
**Created:** [June 10, 2021, 3:49pm UTC](https://discourse.julialang.org/t/does-julia-have-the-python-equivalent-of-valueerror/62684 "2021-06-10T15:49:23Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [June 10, 2021, 3:49pm UTC](https://discourse.julialang.org/t/does-julia-have-the-python-equivalent-of-valueerror/62684/1 "2021-06-10T15:49:23Z")

</div>

In Python, a specific exception exists called [ValueError](https://docs.python.org/3/library/exceptions.html#ValueError):

> Raised when an operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception such as [`IndexError`](https://docs.python.org/3/library/exceptions.html#IndexError).

Does Julia have such an [exception](https://docs.julialang.org/en/v1/manual/control-flow/#Built-in-Exceptions)?

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [June 10, 2021, 3:58pm UTC](https://discourse.julialang.org/t/does-julia-have-the-python-equivalent-of-valueerror/62684/2 "2021-06-10T15:58:17Z")

</div>

I typically use:

```julia
help?> ArgumentError
search: ArgumentError

  ArgumentError(msg)

  The parameters to a function call do not match a valid signature. Argument
  msg is a descriptive error string.

```

in this case.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 10, 2021, 4:27pm UTC](https://discourse.julialang.org/t/does-julia-have-the-python-equivalent-of-valueerror/62684/3 "2021-06-10T16:27:51Z")

</div>

`ArgumentError` is apparently for cases when the _types_ are wrong, but `ValueError` is for when the _values_ are inappropriate, so it doesn’t seem to quite fit.

---

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [June 10, 2021, 4:40pm UTC](https://discourse.julialang.org/t/does-julia-have-the-python-equivalent-of-valueerror/62684/4 "2021-06-10T16:40:27Z")

</div>

I see two options, throw `error()` with a detailed error message and document it or create the `ValueError` exception, and use it.

```julia
struct ValueError <: Exception 
    ...
end 

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [June 10, 2021, 4:52pm UTC](https://discourse.julialang.org/t/does-julia-have-the-python-equivalent-of-valueerror/62684/5 "2021-06-10T16:52:00Z")

</div>

> [@DNF](#):
>
> `ArgumentError` is apparently for cases when the _types_ are wrong

When types are wrong you will get `MethodError` as Julia will not find a matching method for your call.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [June 10, 2021, 4:52pm UTC](https://discourse.julialang.org/t/does-julia-have-the-python-equivalent-of-valueerror/62684/6 "2021-06-10T16:52:03Z")

</div>

`DomainError` is used for this (for some mathematical functions like `sqrt`, `log` etc at least):

```julia
julia> sqrt(-1)
ERROR: DomainError with -1.0:
sqrt will only return a complex result if called with a complex argument.
Try sqrt(Complex(x)).
Stacktrace:

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [June 10, 2021, 4:53pm UTC](https://discourse.julialang.org/t/does-julia-have-the-python-equivalent-of-valueerror/62684/7 "2021-06-10T16:53:19Z")

</div>

Good point.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 10, 2021, 4:53pm UTC](https://discourse.julialang.org/t/does-julia-have-the-python-equivalent-of-valueerror/62684/8 "2021-06-10T16:53:37Z")

</div>

But then what’s up with this docstring?

> The parameters to a function call do not match a valid signature.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [June 10, 2021, 5:07pm UTC](https://discourse.julialang.org/t/does-julia-have-the-python-equivalent-of-valueerror/62684/9 "2021-06-10T17:07:15Z")

</div>

I agree this is a bit fuzzy. I know the practice, which is (just some examples):

```julia
julia> Dates.Date(0,0,0)
ERROR: ArgumentError: Month: 0 out of range (1:12)

julia> parse(Int, 'a')
ERROR: ArgumentError: invalid base 10 digit 'a'

julia> parse(Int, "a")
ERROR: ArgumentError: invalid base 10 digit 'a' in "a"

julia> Symbol("ab\0")
ERROR: ArgumentError: Symbol name may not contain \0

```

and my favorite:

```julia
julia> Base.notnothing(nothing)
ERROR: ArgumentError: nothing passed to notnothing

```

which is defined as:

```julia
notnothing(::Nothing) = throw(ArgumentError("nothing passed to notnothing"))

```

which contradicts the interpretation from the docstring in 100% 😃.

The practice I use in DataFrames.jl:

1. `DomainError` - when working with mathematical functions kind of stuff;
2. `ArgumentError` - all else

In theory (if we read the docstring precisely) `ArgumentError` should be probably used when there is e.g. `::Any` in the signature, but we dynamically check the type within the body of the function, but AFAICT this is not its typical use case in practice.

You also have `BoundsError` which is also kind of `DomainError` but specific for indexing operations.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 10, 2021, 5:33pm UTC](https://discourse.julialang.org/t/does-julia-have-the-python-equivalent-of-valueerror/62684/10 "2021-06-10T17:33:56Z")

</div>

> [@bkamins](#):
>
> which contradicts the interpretation from the docstring in 100% 😃.

I just make a PR to fix this: [clarify ArgumentError docstring by stevengj · Pull Request #41177 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/41177)

---

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [June 10, 2021, 6:34pm UTC](https://discourse.julialang.org/t/does-julia-have-the-python-equivalent-of-valueerror/62684/11 "2021-06-10T18:34:15Z")

</div>

I take it that `ArgumentError` is what I should use, correct?

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [June 10, 2021, 6:39pm UTC](https://discourse.julialang.org/t/does-julia-have-the-python-equivalent-of-valueerror/62684/12 "2021-06-10T18:39:04Z")

</div>

My opinion, as I have commented above, is that `ArgumentError` is often used in such cases. You can also use `DomainError` as @fredrikekre suggested - depending on what you feel is more appropriate in your case.
