1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
require 'timeout'
require 'net/http'
 
begin
  #raise RuntimeError
  timeout(0.5) do
    Net::HTTP.get("twitter.com", "/hyper/heavy/page", 80)
  end
rescue => e
  puts "generic exception!"
  puts e.class, e.message
rescue Timeout::Error => e
  puts "timeout!"
  puts e.class, e.message
end

コメントそのまま

timeout!
Timeout::Error
execution expired

コメントを外すと

generic exception!
RuntimeError
RuntimeError

参考