diff --git a/base/dict.jl b/base/dict.jl index 1c176a2eab864..2c99dabe1a199 100644 --- a/base/dict.jl +++ b/base/dict.jl @@ -128,7 +128,13 @@ function Dict(kv) try dict_with_eltype((K, V) -> Dict{K, V}, kv, eltype(kv)) catch - if !isiterable(typeof(kv)) || !all(x->isa(x,Union{Tuple,Pair}),kv) + valid_iterator = try + isiterable(typeof(kv)) && all(x->isa(x,Union{Tuple,Pair}),kv) + catch + true # An exception has occurred during iteration. Rethrow the original error + end + + if !valid_iterator throw(ArgumentError("Dict(kv): kv needs to be an iterator of tuples or pairs")) else rethrow() diff --git a/test/dict.jl b/test/dict.jl index 534e88ada036c..ba220789ceade 100644 --- a/test/dict.jl +++ b/test/dict.jl @@ -162,6 +162,15 @@ end # issue #39117 @test Dict(t[1]=>t[2] for t in zip((1,"2"), (2,"2"))) == Dict{Any,Any}(1=>2, "2"=>"2") + + @testset "issue #33147" begin + try + Dict(i => error("$i") for i in 1:3) + catch ex + @test ex isa ErrorException + @test length(Base.catch_stack()) == 1 + end + end end @testset "type of Dict constructed from varargs of Pairs" begin