# When to use \`DomainError\` instead of \`ArgumentError\`?

**URL:** <https://discourse.julialang.org/t/when-to-use-domainerror-instead-of-argumenterror/120556>\
**Category:** General Usage\
**Tags:** question, error, error-message\
**Created:** [September 29, 2024, 12:46am UTC](https://discourse.julialang.org/t/when-to-use-domainerror-instead-of-argumenterror/120556 "2024-09-29T00:46:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [September 29, 2024, 12:46am UTC](https://discourse.julialang.org/t/when-to-use-domainerror-instead-of-argumenterror/120556/1 "2024-09-29T00:46:01Z")

</div>

For example, if an algorithm can only be used on square matrices, which error should I throw?

```julia
function square_matrix_algorithm(A::AbstractMatrix)
    if size(A, 1) != size(A, 2)
        throw(ArgumentError("This function can only be used on square matrices!"))
        # Or
        throw(DomainError(A, "This function can only be used on square matrices!"))
    end
    ...
end

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 29, 2024, 12:58am UTC](https://discourse.julialang.org/t/when-to-use-domainerror-instead-of-argumenterror/120556/2 "2024-09-29T00:58:31Z")

</div>

`ArgumentError` is documented vaguely because it’s used for quite a few things, like the wrong number or type of arguments. Arity and type can also be handled with method dispatch and absent methods (`MethodError`), but sometimes things are written with branches in one method.

`DomainError` more specifically deals with a subset of a Julia type’s instances being invalid in a particular context, like negative reals for `sqrt`. In isolation, it does technically apply here, but there is an even more specific `DimensionMismatch` for square matrices.

```julia
julia> using LinearAlgebra

julia> det([0 1
            2 3]) # determinant defined for square matrices
-2.0

julia> det([0 1 10
            2 3 20])
ERROR: DimensionMismatch: matrix is not square: dimensions are (2, 3)

```

As a general rule, the more specific error is proper, but in practice, check similar functions in Base, the standard library, or popular third-party packages, in that order.

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [September 29, 2024, 1:12am UTC](https://discourse.julialang.org/t/when-to-use-domainerror-instead-of-argumenterror/120556/3 "2024-09-29T01:12:26Z")

</div>

Thanks! That’s another error type that I have been wondering how to use for long. Its docs is very vague, too:

```julia
The objects called do not have matching dimensionality.

```

I always use it to compare 2 or more arrays’ sizes:

```julia
if size(A) != size(B)
    throw(DimensionMismatch("..."))
end

```

But I always want to know: if I require a matrix of size 3x3 as argument, should I use `DimensionMismatch` or `ArgumentError`?

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [September 29, 2024, 1:25am UTC](https://discourse.julialang.org/t/when-to-use-domainerror-instead-of-argumenterror/120556/4 "2024-09-29T01:25:56Z")

</div>

> [@singularitti](#):
>
> But I always want to know: if I require a matrix of size 3x3 as argument, should I use `DimensionMismatch` or `ArgumentError`?

I would say that DimensionMismatch is the correct type of error in this case

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 29, 2024, 1:27am UTC](https://discourse.julialang.org/t/when-to-use-domainerror-instead-of-argumenterror/120556/5 "2024-09-29T01:27:27Z")

</div>

^An internal LinearAlgebra example in v1.10.5

```julia
function matmul3x3!(C::AbstractMatrix, tA, tB, A::AbstractMatrix, B::AbstractMatrix,
                    _add::MulAddMul = MulAddMul())
    require_one_based_indexing(C, A, B)
    if !(size(A) == size(B) == size(C) == (3,3))
        throw(DimensionMismatch(lazy"A has size $(size(A)), B has size $(size(B)), C has size $(size(C))"))
    end
    ...

```

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [September 29, 2024, 4:21am UTC](https://discourse.julialang.org/t/when-to-use-domainerror-instead-of-argumenterror/120556/6 "2024-09-29T04:21:28Z")

</div>

> [@Benny](#):
>
> ```julia
> if !(size(A) == size(B) == size(C) == (3,3))
> throw(DimensionMismatch(lazy"A has size $(size(A)), B has size $(size(B)), C has size $(size(C))"))
> end
> 
> ```

Thanks! But I think this `DimensionMismatch` comes from `size(A) == size(B) == size(C)`.

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [September 29, 2024, 7:12am UTC](https://discourse.julialang.org/t/when-to-use-domainerror-instead-of-argumenterror/120556/7 "2024-09-29T07:12:50Z")

</div>

Also, how about this? Should I use `DomainError`?

```julia
if radius < zero(radius)
    throw(DomainError(radius, "radius must be non-negative!"))
end

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 29, 2024, 8:51am UTC](https://discourse.julialang.org/t/when-to-use-domainerror-instead-of-argumenterror/120556/8 "2024-09-29T08:51:03Z")

</div>

> [@singularitti](#):
>
> I think this `DimensionMismatch` comes from `size(A) == size(B) == size(C)`.

The `== (3,3)` right after it in the same line is also a critical trigger of the `DimensionMisMatch`, and the function name includes `3x3` to indicate its intent. Note that `det` only takes 1 matrix, so “mismatch” doesn’t inherently compare multiple matrices, it refers to any unsuitable dimensions.

> [@singularitti](#):
>
> ```julia
> if radius < zero(radius)
> throw(DomainError(radius, "radius must be non-negative!"))
> end
> 
> ```

That seems right to me, and Base has the same check.
