我經常發現自己處理這些類型的場景: require 'nokogiri'
require "open-uri"
url = "https://www.random_website.com/contains_info_I_want_to_parse"
nokodoc = Nokogiri::HTML(open(url))
# Let's say one of the following l
require 'json'
begin
hash = {"a" => "b"}
raise StandardError, hash
rescue Exception => e
q = e.message
p q
p q.to_json
end
它應該打印"{\"a\":\"b\"}"但它打印"\"{\\\"a\\\"=>\\\"b\\\
我需要在特殊情況下捕獲NameError。但我不想捕獲NameError的所有子類。有沒有辦法做到這一點? begin
# your code goes here
rescue NameError => exception
# note that `exception.kind_of?` will not work as expected here
raise un