1

我有一段很好的代码。我试图在推特上发布相同的文本,但我的脚本结束了,因为/lib/ruby/gems/1.8/gems/twitter-0.9.4/lib/twitter.rb:87:in 'raise_errors': (403): Forbidden - Status is a duplicate. (Twitter::General)

我知道我不能两次发布相同的文本,但我认为我会在响应变量中得到错误。

我该如何处理错误?所以我的脚本会很好地完成而不是因为错误?

oauth = Twitter::OAuth.new('consumer token', 'consumer secret')
oauth.authorize_from_access('access token', 'access secret')

client = Twitter::Base.new(oauth)
response = client.update('Heeeyyyyoooo from Twitter Gem!')
4

1 回答 1

2

您可以将任何 ruby​​ 语句或语句块包装在begin.. rescue..end中以捕获错误 - 您可能想试试这个:

begin
  oauth = Twitter::OAuth.new('consumer token', 'consumer secret')
  oauth.authorize_from_access('access token', 'access secret')

  client = Twitter::Base.new(oauth)
  response = client.update('Heeeyyyyoooo from Twitter Gem!')
rescue Twitter::General
  # Catch the error and do nothing
end

如果您想发现任何错误,您可以将救援线更改为rescue. 您可以在 ruby​​-doc 网站上阅读有关它们的更多信息。

于 2010-04-21T14:12:38.560 回答