Skip to content

Instantly share code, notes, and snippets.

@emmx

emmx/foo.rb Secret

Last active April 16, 2016 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emmx/0178dc758501bdfe238a36ab69b9d69b to your computer and use it in GitHub Desktop.
Save emmx/0178dc758501bdfe238a36ab69b9d69b to your computer and use it in GitHub Desktop.
Thread.abort_on_exception = true
begin
t1 = Thread.new do
t2 = Thread.new do
sleep 5
puts "RAISE"
1/0
end
sleep 3
puts "WAITING T2"
t2.join
end
sleep 1
puts "WAITING T1"
t1.join
rescue => e
puts "ERROR "+e.message
end
puts "FINISHED"
########################
# TWO POSSIBLE OUTPUTS
########################
#
# Case A
#
# WAITING T1
# WAITING T2
# RAISE
# ERROR divided by 0test.rb:8:in `/': divided by 0 (ZeroDivisionError)
# from test.rb:8:in `block (2 levels) in <main>'
#
########################
#
# Case B
#
# WAITING T1
# WAITING T2
# RAISE
# ERROR divided by 0
# FINISHED
#
########################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment