Skip to content

Commit a2c60af

Browse files
Throw ArgumentError not plain ErrorException
1 parent 3218d9d commit a2c60af

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

base/iterators.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,29 +1097,31 @@ length(s::Stateful) = length(s.itr) - s.taken
10971097
"""
10981098
only(x)
10991099
1100-
Returns the one and only element of collection `x`, and throws an error if the collection
1101-
has zero or multiple elements.
1100+
Returns the one and only element of collection `x`, and throws an `ArgumentError` if the
1101+
collection has zero or multiple elements.
11021102
"""
11031103
Base.@propagate_inbounds function only(x)
11041104
i = iterate(x)
11051105
@boundscheck if i === nothing
1106-
error("Collection is empty, must contain exactly 1 element")
1106+
throw(ArgumentError("Collection is empty, must contain exactly 1 element"))
11071107
end
11081108
(ret, state) = i
11091109
@boundscheck if iterate(x, state) !== nothing
1110-
error("Collection has multiple elements, must contain exactly 1 element")
1110+
throw(ArgumentError("Collection has multiple elements, must contain exactly 1 element"))
11111111
end
11121112
return ret
11131113
end
11141114

11151115
# Collections of known size
1116-
only(x::Tuple{}) = error("Tuple is empty, must contain exactly 1 element")
1116+
only(x::Tuple{}) = throw(ArgumentError("Tuple is empty, must contain exactly 1 element")
11171117
only(x::Tuple{Any}) = x[1]
1118-
only(x::Tuple) = error("Tuple contains $(length(x)) elements, must contain exactly 1 element")
1119-
1118+
only(x::Tuple) = throw(
1119+
ArgumentError("Tuple contains $(length(x)) elements, must contain exactly 1 element")
1120+
)
11201121
only(a::AbstractArray{<:Any, 0}) = @inbounds return a[]
1121-
11221122
only(x::NamedTuple{<:Any, <:Tuple{Any}}) = first(x)
1123-
only(x::NamedTuple) = error("NamedTuple contains $(length(x)) elements, must contain exactly 1 element")
1123+
only(x::NamedTuple) = throw(
1124+
ArgumentError("NamedTuple contains $(length(x)) elements, must contain exactly 1 element")
1125+
)
11241126

11251127
end

test/iterators.jl

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ end
183183
@test Base.IteratorEltype(repeated(0, 5)) == Base.HasEltype()
184184
@test Base.IteratorSize(zip(repeated(0), repeated(0))) == Base.IsInfinite()
185185

186-
187186
# product
188187
# -------
189188

@@ -411,7 +410,6 @@ for n in [5,6]
411410
[(1,1),(2,2),(3,3),(4,4),(5,5)]
412411
end
413412

414-
415413
@test join(map(x->string(x...), partition("Hello World!", 5)), "|") ==
416414
"Hello| Worl|d!"
417415

@@ -650,22 +648,22 @@ end
650648

651649
@testset "only" begin
652650
@test only([3]) === 3
653-
@test_throws ErrorException only([])
654-
@test_throws ErrorException only([3, 2])
651+
@test_throws ArgumentError only([])
652+
@test_throws ArgumentError only([3, 2])
655653

656654
@test @inferred(only((3,))) === 3
657-
@test_throws ErrorException only(())
658-
@test_throws ErrorException only((3, 2))
655+
@test_throws ArgumentError only(())
656+
@test_throws ArgumentError only((3, 2))
659657

660658
@test only(Dict(1=>3)) === (1=>3)
661-
@test_throws ErrorException only(Dict{Int,Int}())
662-
@test_throws ErrorException only(Dict(1=>3, 2=>2))
659+
@test_throws ArgumentError only(Dict{Int,Int}())
660+
@test_throws ArgumentError only(Dict(1=>3, 2=>2))
663661

664662
@test only(Set([3])) === 3
665-
@test_throws ErrorException only(Set(Int[]))
666-
@test_throws ErrorException only(Set([3,2]))
663+
@test_throws ArgumentError only(Set(Int[]))
664+
@test_throws ArgumentError only(Set([3,2]))
667665

668666
@test @inferred(only((;a=1))) === 1
669-
@test_throws ErrorException only(NamedTuple())
670-
@test_throws ErrorException only((a=3, b=2.0))
667+
@test_throws ArgumentError only(NamedTuple())
668+
@test_throws ArgumentError only((a=3, b=2.0))
671669
end

0 commit comments

Comments
 (0)