# Override the error message for avector\[0\]?

**URL:** https://discourse.julialang.org/t/override-the-error-message-for-avector-0/74306
**Category:** Internals & Design
**Tags:** indexing, arrays, error-message
**Created:** [January 9, 2022, 8:59pm UTC](https://discourse.julialang.org/t/override-the-error-message-for-avector-0/74306 "2022-01-09T20:59:14Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [January 9, 2022, 8:59pm UTC](https://discourse.julialang.org/t/override-the-error-message-for-avector-0/74306/1 "2022-01-09T20:59:14Z")

</div>

Proposal: override the error message for avector[0] with a message that specify explicitly that array indexing starts at 1 in Julia ??

I know: “If we do it for this, why not for …” but it is such a common error for people coming from C/Java/Python, that maybe it deserves a reflection (or maybe it has saready been discussed with a conclusion not to implement it, that’s fine…)

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [January 9, 2022, 9:09pm UTC](https://discourse.julialang.org/t/override-the-error-message-for-avector-0/74306/2 "2022-01-09T21:09:16Z")

</div>

> [@sylvaticus](#):
>
> array indexing starts at 1 in Julia

Does it?

> **[GitHub - giordano/StarWarsArrays.jl: Arrays indexed as the order of Star Wars...](https://github.com/giordano/StarWarsArrays.jl)**
>
> Arrays indexed as the order of Star Wars movies. Contribute to giordano/StarWarsArrays.jl development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [January 9, 2022, 9:10pm UTC](https://discourse.julialang.org/t/override-the-error-message-for-avector-0/74306/3 "2022-01-09T21:10:20Z")

</div>

well, we could append the error message with:

```julia
, the first index is $(firstindex(ary))

```

(special case `0` sounds okay, but most of the time it’s not useful because people won’t hard-code `0` over and over)

the language shouldn’t be optimized for first-time user and the goal is never to make Julia “you can use it without ever reading docs or googleing and just fix what error message says”

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [January 9, 2022, 9:13pm UTC](https://discourse.julialang.org/t/override-the-error-message-for-avector-0/74306/4 "2022-01-09T21:13:33Z")

</div>

Without wanting to muddy the thread, these two cases’ error msgs are too similar for being so different:

```julia
julia> Int[1][0]
ERROR: BoundsError: attempt to access 1-element Vector{Int64} at index [0]

julia> Int[][0]
ERROR: BoundsError: attempt to access 0-element Vector{Int64} at index [0]

```

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [January 9, 2022, 10:29pm UTC](https://discourse.julialang.org/t/override-the-error-message-for-avector-0/74306/5 "2022-01-09T22:29:00Z")

</div>

It seems like the problem is that the error reports the size of the array but given OffsetArrays etc, in order to understand bounds-checking failure you need to know the bounds, not the size, of the array.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [January 10, 2022, 6:16am UTC](https://discourse.julialang.org/t/override-the-error-message-for-avector-0/74306/6 "2022-01-10T06:16:01Z")

</div>

Something like this maybe

```julia
ERROR: BoundsError: attempt to access at index 0, valid indices are 1:231

```

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [January 10, 2022, 12:12pm UTC](https://discourse.julialang.org/t/override-the-error-message-for-avector-0/74306/7 "2022-01-10T12:12:46Z")

</div>

That’s a nice, clear message. 👍

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [January 10, 2022, 1:22pm UTC](https://discourse.julialang.org/t/override-the-error-message-for-avector-0/74306/8 "2022-01-10T13:22:41Z")

</div>

I wonder if it’s more likely for them to know syntax `1:231` or that Julia is 1-based indexing.

In any case, we need to use `firstindex()` and `lastindex()` to generate that message in case OffsetArrays or similar is involved, but even then, some one may have a skipping-index array? (does this conflict with array interface?)

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [January 10, 2022, 2:26pm UTC](https://discourse.julialang.org/t/override-the-error-message-for-avector-0/74306/9 "2022-01-10T14:26:03Z")

</div>

I would hope this would be a more general message than just `"$(firstindex(x)):$(lastindex(x))" `. Perhaps a `showindices(io, x)` that defaults to `show(io, eachindex(x))` (perhaps with some cosmetic modification to things like `Base.OneTo`).

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [January 10, 2022, 2:36pm UTC](https://discourse.julialang.org/t/override-the-error-message-for-avector-0/74306/10 "2022-01-10T14:36:01Z")

</div>

> [@jzr](#):
>
> problem is that the error reports the size of the array but given OffsetArrays etc

OffsetArrays already print something sensible:

```julia
julia> OffsetArray([1,2],3)[6]
ERROR: BoundsError: attempt to access 2-element OffsetArray(::Vector{Int64}, 4:5) with eltype Int64 with indices 4:5 at index [6]

julia> OffsetArray([1 2; 3 4],5,6)[6]
ERROR: BoundsError: attempt to access 2×2 Matrix{Int64} at index [6]

julia> OffsetArray([1 2; 3 4],5,6)[7,9]
ERROR: BoundsError: attempt to access 2×2 OffsetArray(::Matrix{Int64}, 6:7, 7:8) with eltype Int64 with indices 6:7×7:8 at index [7, 9]

```

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [January 10, 2022, 3:23pm UTC](https://discourse.julialang.org/t/override-the-error-message-for-avector-0/74306/11 "2022-01-10T15:23:41Z")

</div>

As does `StarWarsArrays`:

```julia
julia> v = StarWarsArray(1:6, MacheteOrder)
5-element view(::UnitRange{Int64}, [3, 4, 1, 2, 5]) with eltype Int64:
 3
 4
 1
 2
 5

julia> v[1]
ERROR: StarWarsError: there is no episode 1 in MacheteOrder

```

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [January 10, 2022, 3:33pm UTC](https://discourse.julialang.org/t/override-the-error-message-for-avector-0/74306/12 "2022-01-10T15:33:38Z")

</div>

I still like @gustaphe’s suggestion; we could do something like that for the default message in which case OffsetArrays may not even need to customize it. That said, it can be helpful to know the size and type of the array in figuring out which one errored… but that’s kinda secondary to the most important part (the attempted index and the valid indices).

It could also be helpful to more directly talk about _what is wrong_ when there are the wrong number of indices for a multidimensional array or _which dimension_ is the troublesome one.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [January 10, 2022, 5:12pm UTC](https://discourse.julialang.org/t/override-the-error-message-for-avector-0/74306/13 "2022-01-10T17:12:20Z")

</div>

```julia
showindices(io, x::MyRealIndexedObject) = print(io, "reals in [", lowerindex(x), ", ", upperindex(x), ")")

```

```julia
julia> x[1.25]
BoundsError: tried to access MyRealIndexedObject at index 1.25, legal indices are reals in [0.0, 1.0).

```

This might be overkill, but see what a bit of generality can do. Allowing some customization on this message would mean it’s more likely to be user friendly, rather than technically perfectly accurate.

---

<div class="post-metadata">

### Author: ![arik](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@arik](https://discourse.julialang.org/u/arik)
#### Post date: [January 17, 2022, 1:34pm UTC](https://discourse.julialang.org/t/override-the-error-message-for-avector-0/74306/14 "2022-01-17T13:34:19Z")

</div>

What about `Base.Experimental.register_error_hint`? I know its experimental, but this could be readily used used to implement such hints:

```julia

Base.Experimental.register_error_hint(BoundsError) do io, ex
    arrayDim(::Array{T,N}) where {T,N} = N
    if ex.a isa Array && arrayDim(ex.a)>0 && 0 in ex.i
        printstyled(io,"\nHint: In Julia, the standart Arrays start at 1", color=:cyan)
    end
end

a=[1,2]
a[0]

BoundsError: attempt to access 2-element Vector{Int64} at index [0]
Hint: In Julia, the standart Arrays start at 1

Stacktrace:
.....

```

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [January 17, 2022, 3:15pm UTC](https://discourse.julialang.org/t/override-the-error-message-for-avector-0/74306/15 "2022-01-17T15:15:50Z")

</div>

Unfortunately, it only takes the type as an argument, so it can’t tell you things like the maximum index for a specific instance.

---

<div class="post-metadata">

### Author: ![arik](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@arik](https://discourse.julialang.org/u/arik)
#### Post date: [January 18, 2022, 1:47pm UTC](https://discourse.julialang.org/t/override-the-error-message-for-avector-0/74306/16 "2022-01-18T13:47:07Z")

</div>

Yes it can! When a BoundsError is thrown, the errorhandler you defined for `register_error_hint` gets passed the thrown Exception (the argument `ex` in my example), and the Exception itself contains references to both the indices and the object (array) that was accessed (as `ex.a` for the array and `ex.i` for the indices, as a Tuple).

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [January 18, 2022, 6:22pm UTC](https://discourse.julialang.org/t/override-the-error-message-for-avector-0/74306/17 "2022-01-18T18:22:03Z")

</div>

Aw snap. Good you caught that before I finished my PR.
