Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

which field is causing the InexactError? #26475

Closed
ggggggggg opened this issue Mar 15, 2018 · 3 comments
Closed

which field is causing the InexactError? #26475

ggggggggg opened this issue Mar 15, 2018 · 3 comments
Labels
error handling Handling of exceptions by Julia or the user error messages Better, more actionable error messages

Comments

@ggggggggg
Copy link
Contributor

Consider

julia> struct A
           a::Int
           b::Vector{UInt16}
       end;
julia> foo()=A(1.0,[1.1,2]);
julia> bar()=A(1.1,[1,2]);
julia> foo()
ERROR: InexactError()
Stacktrace:
 [1] convert(::Type{UInt16}, ::Float64) at ./float.jl:658
 [2] copy!(::IndexLinear, ::Array{UInt16,1}, ::IndexLinear, ::Array{Float64,1}) at ./abstractarray.jl:655
 [3] foo() at ./REPL[22]:1
julia> bar()
ERROR: InexactError()
Stacktrace:
 [1] convert(::Type{Int64}, ::Float64) at ./float.jl:679
 [2] Type at ./REPL[21]:2 [inlined]
 [3] bar() at ./REPL[23]:1

These error messages are pretty hard to interpret, you need to at look at the line indicated in [3] to figure out that the problem is in a call to a constructor of A. Then it's not clear what field is involved. It would be really nice if the call signature to A was shown (as is the case if I don't wrap these in functions), eg

julia> A(1.0,[1.1,2])
ERROR: InexactError()
Stacktrace:
 [1] convert(::Type{UInt16}, ::Float64) at ./float.jl:658
 [2] copy!(::IndexLinear, ::Array{UInt16,1}, ::IndexLinear, ::Array{Float64,1}) at ./abstractarray.jl:655
 [3] A(::Float64, ::Array{Float64,1}) at ./REPL[21]:2

and it would be extra nice if there was a line that indicated the which field was involved. In this case it's actually not too hard to figure out which field is the problem from the messages, but for a struct with lots of fields deep in some code, it can feel very opaque.

@fredrikekre
Copy link
Member

Note that this has been changed on master (#20005) and InexactError now gives the following information:

julia> foo()
ERROR: InexactError: UInt16(UInt16, 1.1)
Stacktrace:
 [1] Type at ./float.jl:668 [inlined]
 [2] convert at ./number.jl:7 [inlined]
 [3] setindex! at ./array.jl:699 [inlined]
 [4] copyto!(::IndexLinear, ::Array{UInt16,1}, ::IndexLinear, ::Array{Float64,1}) at ./abstractarray.jl:711
 [5] copyto! at ./abstractarray.jl:703 [inlined]
 [6] Type at ./array.jl:426 [inlined]
 [7] convert at ./array.jl:418 [inlined]
 [8] Type at ./REPL[1]:2 [inlined]
 [9] foo() at ./REPL[2]:1
 [10] top-level scope

julia> bar()
ERROR: InexactError: Int64(Int64, 1.1)
Stacktrace:
 [1] Type at ./float.jl:689 [inlined]
 [2] convert at ./number.jl:7 [inlined]
 [3] Type at ./REPL[1]:2 [inlined]
 [4] bar() at ./REPL[3]:1
 [5] top-level scope

@JeffBezanson JeffBezanson added the error handling Handling of exceptions by Julia or the user label Mar 20, 2018
@JeffBezanson
Copy link
Member

The printing UInt16(UInt16, 1.1) is pretty weird. We should also work on replacing Type with the name of the type being constructed in the stacktrace.

@brenhinkeller brenhinkeller added the error messages Better, more actionable error messages label Nov 19, 2022
@brenhinkeller
Copy link
Contributor

brenhinkeller commented Nov 19, 2022

The current (1.8.3) error message seems pretty good to me now:

julia> struct A
           a::Int
           b::Vector{UInt16}
       end

julia> foo()=A(1.0,[1.1,2]);

julia> bar()=A(1.1,[1,2]);

julia> foo()
ERROR: InexactError: UInt16(1.1)
Stacktrace:
  [1] UInt16
    @ ./float.jl:767 [inlined]
  [2] convert
    @ ./number.jl:7 [inlined]
 ...
 [13] foo()
    @ Main ./REPL[2]:1
 [14] top-level scope
    @ REPL[4]:1

which makes it immediately clear that the problem is specifically trying to call the UInt16 constructor on the value 1.1

Similarly for bar:

julia> bar()
ERROR: InexactError: Int64(1.1)
Stacktrace:
 [1] Int64
   @ ./float.jl:788 [inlined]
 [2] convert
   @ ./number.jl:7 [inlined]
 [3] A(a::Float64, b::Vector{Int64})
   @ Main ./REPL[1]:2
 [4] bar()
   @ Main ./REPL[3]:1
 [5] top-level scope
   @ REPL[5]:1

I think that's good enough we can consider this completed finally, but of course reopen if anyone disagrees!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error handling Handling of exceptions by Julia or the user error messages Better, more actionable error messages
Projects
None yet
Development

No branches or pull requests

4 participants