Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7410539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:14:56+00:00 2026-05-29T06:14:56+00:00

I seem to be reusing the same rescue s. Would it be possible to

  • 0

I seem to be reusing the same rescues. Would it be possible to have a rescue block? So instead of:

while count != 0 do 
<<code>>
rescue error1
<<code>>
retry
rescue error2
<<code>>
break
end

I can have:

def rescue_block
rescue error 1
<<code>>
retry
rescue error 2
<<code>>
break
end

while count !=0
<<code>>
rescue_block
end

Notice the break and retry. The rescue_block actions need to apply to the loop, not itself.

EDIT:

Using Twitter gem, most of Twitter’s errors can be handled the same way (i.e. if you hit the API limit, wait.). Full code:

while cursor != 0 do
  begin
    grabFollowers ? f = Twitter.follower_ids(username,{:cursor=>cursor}) : f = Twitter.friend_ids(username,{:cursor=>cursor})
    outfile.puts(f.ids) if writeHumanReadable
    arrayOfIds << f.ids
    cursor = f.next_cursor
  rescue Twitter::Error::Unauthorized
    break
  rescue Twitter::Error::BadRequest
    #ran out of OAUTH calls, waits until OAUTH reset + 10 seconds.
    outErrorFile = File.new("#{username.to_s}-ERROR.txt",'w')
    resetTime = Twitter.rate_limit_status.reset_time_in_seconds
    current = Time.now.to_i
    pauseDuration = resetTime-current+10
    outErrorFile.puts(Time.now)
    outErrorFile.puts(pauseDuration/60)
    outErrorFile.close
    sleep(pauseDuration)
    retry
  rescue Twitter::Error::NotFound
    break
  rescue Twitter::Error::ServiceUnavailable
    sleep(60*5)
    retry
  end
  sleep(2)
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:14:56+00:00Added an answer on May 29, 2026 at 6:14 am

    Your example makes it look like you want macros, which Ruby doesn’t have.

    We can get pretty close to your example with blocks, but it is really difficult to answer this without knowing your use case. (I imagine you aren’t using exceptions for flow control, this is typically frowned upon as flow control isn’t an exceptional situation).

    Depending on what you’re trying to do, throw and catch may be better suited to your needs than exceptions.

    class Example
      RetryException = Class.new StandardError
      BreakException = Class.new StandardError
    
      attr_accessor :count
    
      def initialize
        self.count = 10
      end
    
      def rescue_block
        yield
      rescue RetryException
        self.count -= 1
        retry
      rescue BreakException
        self.count -= 2
        return
      end
    
      def count_down
        rescue_block { yield count while 0 < count }
      end
    end
    
    Example.new.count_down do |count|
      count # => 10, 9, 8, 7, 6, 5
      raise Example::BreakException if count == 5
      raise Example::RetryException
    end
    
    Example.new.count_down do |count|
      count # => 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
      raise Example::RetryException
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I seem to always have problems with converting data to and from XML in
This may seem totally unreasonable to ask, but I have been designing a multi-panel,
I know they're using a jQuery plugin, but I can't seem to find which
Seem to be having an issue with std::auto_ptr and assignment, such that the object
I seem to be missing something about LINQ. To me, it looks like it's
There seem to be many ways to define singletons in Python. Is there a
I seem to remember reading something about how it is bad for structs to
I seem right now to be embroiled in a debate with another programmer on
I seem to make this mistake every time I set up a new development
There seem to many ways to skin this particular cat - but which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.