[英]In Ruby, can you create a rescue for twitter for when there is an error it will continue the loop?
我正在嘗試創建一種rescue
,如果出現Twitter::Error::NotFound
錯誤(例如不存在)時,它將繼續循環。 請幫忙,謝謝。
下面是代碼,
begin
File.open("user_ids.txt") do |file|
file.each do |id|
puts client.user("#{id}").screen_name
rescue Twitter::Error::NotFound => error
next # skip this item
end
end
end
除了retry
方法外,還有一種方法可以跳過並繼續前進到循環中的下一個項目? 我很確定error.rate_limit
不適用(我從另一個救援調用中復制了此代碼),是否有另一種調用方法? 像error.notfound.continue_with_loop
我想創建一個挽救工具,如果並且當出現錯誤(例如does not exist
,它將繼續循環。 請幫忙,謝謝。
是的next
將繼續並在循環中重試下一個項目。 retry
將使用相同的項目重試循環。
注意:對於該方法中的所有do
,您沒有足夠的end
。 所以我會嘗試:
begin
File.open("user_ids.txt") do |file|
file.each do |id|
puts client.user("#{id}").screen_name
rescue Twitter::Error::NotFound => error
sleep error.rate_limit.reset_in + 1
next # skip this item
end
end
end
注:見縮進如何正確清楚,當你缺少end
?
您可能需要將當前很多的開始/結束塊轉移到您要從中搶救的代碼周圍(否則它將默認為外部開始/結束而不是循環)
File.open("user_ids.txt") do |file|
file.each do |id|
begin
puts client.user("#{id}").screen_name
rescue Twitter::Error::NotFound => error
sleep error.rate_limit.reset_in + 1
next # skip this item
end
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.