Skip to content

Commit

Permalink
Export catch_stack() for access to exception stack
Browse files Browse the repository at this point in the history
  • Loading branch information
c42f committed Sep 18, 2018
1 parent 8b463fb commit bcc7bab
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ export
# errors
backtrace,
catch_backtrace,
catch_stack,
error,
rethrow,
retry,
Expand Down
44 changes: 22 additions & 22 deletions test/exceptions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,41 @@ using Test
try
error("A")
catch
@test length(Base.catch_stack()) == 1
@test length(catch_stack()) == 1
end
@test length(Base.catch_stack()) == 0
@test length(catch_stack()) == 0
try
try
error("A")
finally
@test length(Base.catch_stack()) == 1
@test length(catch_stack()) == 1
end
catch
@test length(Base.catch_stack()) == 1
@test length(catch_stack()) == 1
end
# Errors stack up
try
error("RootCause")
catch
@test length(Base.catch_stack()) == 1
@test length(catch_stack()) == 1
try
error("B")
catch
stack = Base.catch_stack()
stack = catch_stack()
@test length(stack) == 2
@test stack[1][1].msg == "RootCause"
@test stack[2][1].msg == "B"
end
# Stack pops correctly
stack = Base.catch_stack()
stack = catch_stack()
@test length(stack) == 1
@test stack[1][1].msg == "RootCause"
end
# Lowering - value position
val = try
error("A")
catch
@test length(Base.catch_stack()) == 1
@test length(catch_stack()) == 1
1
end
@test val == 1
Expand All @@ -48,19 +48,19 @@ using Test
try
error("A")
catch
length(Base.catch_stack())
length(catch_stack())
end
end
@test test_exc_stack_tailpos() == 1
@test length(Base.catch_stack()) == 0
@test length(catch_stack()) == 0
end

@testset "Exception stacks and gotos" begin
function test_exc_stack_catch_return()
try
error("A")
catch
@test length(Base.catch_stack()) == 1
@test length(catch_stack()) == 1
return
end
end
Expand All @@ -69,26 +69,26 @@ end
try
error("A")
catch
@test length(Base.catch_stack()) == 1
@test length(catch_stack()) == 1
break
end
end
for i=1:1
try
error("A")
catch
@test length(Base.catch_stack()) == 1
@test length(catch_stack()) == 1
continue
end
end
try
error("A")
catch
@test length(Base.catch_stack()) == 1
@test length(catch_stack()) == 1
@goto outofcatch
end
@label outofcatch
@test length(Base.catch_stack()) == 0
@test length(catch_stack()) == 0
end

@testset "Deep exception stacks" begin
Expand All @@ -106,9 +106,9 @@ end
@test try
test_exc_stack_deep(100)
catch
length(Base.catch_stack())
length(catch_stack())
end == 100
@test length(Base.catch_stack()) == 0
@test length(catch_stack()) == 0
end

@testset "Exception stacks and Task switching" begin
Expand All @@ -125,10 +125,10 @@ end
@test t.state == :done
@test t.result == ErrorException("B")
# Task exception state is preserved around task switches
@test length(Base.catch_stack()) == 1
@test Base.catch_stack()[1][1] == ErrorException("A")
@test length(catch_stack()) == 1
@test catch_stack()[1][1] == ErrorException("A")
end
@test length(Base.catch_stack()) == 0
@test length(catch_stack()) == 0
# test rethrow() rethrows correct state
bt = []
try
Expand All @@ -151,7 +151,7 @@ end
@test exc == ErrorException("A")
@test bt == catch_backtrace()
end
@test length(Base.catch_stack()) == 0
@test length(catch_stack()) == 0
# test rethrow with argument
bt = []
try
Expand All @@ -173,7 +173,7 @@ end
@test exc == ErrorException("C")
@test bt == catch_backtrace()
end
@test length(Base.catch_stack()) == 0
@test length(catch_stack()) == 0
end

@testset "rethrow" begin
Expand Down

0 comments on commit bcc7bab

Please sign in to comment.