以下代码在ruby 2.0和2.1之间的行为有所不同:
require 'timeout'
def scp
begin
puts "In begin"
sleep 10
rescue Exception => e
puts "Exception found: #{e}"
else
puts "No exception found"
ensure
puts "In ensure"
end
end
Timeout::timeout(1) do
scp
end
在ruby 2.0中,它给出了:
In begin
Exception found: execution expired
In ensure
在ruby 2.1中,它给出了:
In begin
In ensure
t2.rb:7:in `sleep': execution expired (Timeout::Error)
from t2.rb:7:in `scp'
from t2.rb:18:in `block in <main>'
from /usr/local/rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/timeout.rb:91:in `block in timeout'
from /usr/local/rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/timeout.rb:35:in `block in catch'
from /usr/local/rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/timeout.rb:35:in `catch'
from /usr/local/rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/timeout.rb:35:in `catch'
from /usr/local/rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/timeout.rb:106:in `timeout'
from t2.rb:17:in `<main>'
为什么他们不同?