-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Open
Labels
error handlingHandling of exceptions by Julia or the userHandling of exceptions by Julia or the usermathsMathematical functionsMathematical functionsrationalsThe Rational type and values thereofThe Rational type and values thereof
Description
The fact that dividing zero by zero throws DivideError in some cases, but ArgumentError in other cases seems inconsistent:
julia> 0 // 0
ERROR: ArgumentError: invalid rational: zero(Int64)//zero(Int64)
Stacktrace:
[1] __throw_rational_argerror_zero(T::Type)
@ Base ./rational.jl:30
[2] Rational
@ ./rational.jl:32 [inlined]
[3] Rational
@ ./rational.jl:48 [inlined]
[4] //(n::Int64, d::Int64)
@ Base ./rational.jl:84
[5] top-level scope
@ REPL[15]:1
julia> (0 // 1) // 0
ERROR: DivideError: integer division error
Stacktrace:
[1] div
@ ./int.jl:297 [inlined]
[2] div
@ ./int.jl:229 [inlined]
[3] divgcd(x::Int64, y::Int64)
@ Base ./rational.jl:54
[4] //(x::Rational{Int64}, y::Int64)
@ Base ./rational.jl:87
[5] top-level scope
@ REPL[16]:1FWIW:
julia> Base.infer_exception_type(//, Tuple{Int, Int})
Union{DivideError, ArgumentError, OverflowError}
julia> Base.infer_exception_type(//, Tuple{Int, Rational{Int}})
Union{DivideError, OverflowError}
julia> Base.infer_exception_type(//, Tuple{Rational{Int}, Int})
Union{DivideError, OverflowError}
julia> Base.infer_exception_type(//, Tuple{Rational{Int}, Rational{Int}})
Union{DivideError, OverflowError}Should 0 // 0 be changed to throw DivideError? Should (0 // 1) // 0 be changed to throw ArgumentError?
The specific reason I'm interested in this inconsistency is that I'm implementing a type very similar to Rational in a package. So I need to decide what to throw there.
Metadata
Metadata
Assignees
Labels
error handlingHandling of exceptions by Julia or the userHandling of exceptions by Julia or the usermathsMathematical functionsMathematical functionsrationalsThe Rational type and values thereofThe Rational type and values thereof