begin...rescue...retry next?

Discussion in 'Ruby' started by Joe Ruby, Aug 16, 2006.

  1. Joe Ruby

    Joe Ruby Guest

    Is there some way to do this in Ruby?

    begin
    # do stuff, uh-oh an exception gets raised
    rescue
    retry next
    end

    There's 'retry' which returns to the same statement that threw the
    exception, but I'd like execution to resume at the next statement
    following it.

    Thanks,
    Joe

    --
    Posted via http://www.ruby-forum.com/.
     
    Joe Ruby, Aug 16, 2006
    #1
    1. Advertising

  2. Joe Ruby

    Max Muermann Guest

    > There's 'retry' which returns to the same statement that threw the
    > exception, but I'd like execution to resume at the next statement
    > following it.
    >


    not_tried_before = true
    begin
    do_dangerous_stuff if not_tried_before
    rescue
    not_tried_before = false
    retry
    end

    Max
     
    Max Muermann, Aug 16, 2006
    #2
    1. Advertising

  3. On Aug 15, 2006, at 10:26 PM, Joe Ruby wrote:

    > Is there some way to do this in Ruby?
    >
    > begin
    > # do stuff, uh-oh an exception gets raised
    > rescue
    > retry next
    > end
    >
    > There's 'retry' which returns to the same statement that threw the
    > exception, but I'd like execution to resume at the next statement
    > following it.
    >
    > Thanks,
    > Joe
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >


    If you know only expression X will raise the exception you can use
    the modifier form

    some_code
    some_exception_raising_call rescue nil # This is evil
    continue_blissfully_unaware_of_whether_an_exception_was_raised

    Also retry does not return to the statement that raised the
    exception, it jumps to the begin

    begin
    # retry will put you here
    some_code
    some_more_code
    raise_an_exception
    even_more_code
    rescue
    retry
    end
     
    Logan Capaldo, Aug 16, 2006
    #3
  4. Joe Ruby

    Joe Ruby Guest

    Logan Capaldo wrote:
    > some_exception_raising_call rescue nil # This is evil


    Why is that evil?

    Joe

    --
    Posted via http://www.ruby-forum.com/.
     
    Joe Ruby, Aug 16, 2006
    #4
  5. On Aug 16, 2006, at 12:41 AM, Joe Ruby wrote:

    > Logan Capaldo wrote:
    >> some_exception_raising_call rescue nil # This is evil

    >
    > Why is that evil?
    >
    > Joe
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >


    Cause you can't specify which exceptions to catch with that form.
    Someday something terrible could happen inside
    some_exception_raising_call that you _do_ want to know about but
    it'll just get thrown away by the rescue nil.
     
    Logan Capaldo, Aug 16, 2006
    #5
  6. Joe Ruby wrote:
    > Logan Capaldo wrote:
    >
    >> some_exception_raising_call rescue nil # This is evil
    >>

    >
    > Why is that evil?
    >
    > Joe
    >


    Because if something goes wrong in that call, you will get no
    information about it. Won't even know it happened.

    -Justin
     
    Justin Collins, Aug 16, 2006
    #6
  7. Joe Ruby

    Joe Ruby Guest

    Justin Collins wrote:
    > Joe Ruby wrote:
    >> Logan Capaldo wrote:
    >>
    >>> some_exception_raising_call rescue nil # This is evil
    >>>

    >>
    >> Why is that evil?
    >>
    >> Joe
    >>

    >
    > Because if something goes wrong in that call, you will get no
    > information about it. Won't even know it happened.
    >
    > -Justin


    Yeah, but rescue nil is just usually to deal with null values from the
    database, for example, which isn't a big deal.

    Joe

    --
    Posted via http://www.ruby-forum.com/.
     
    Joe Ruby, Aug 16, 2006
    #7
  8. Joe Ruby wrote:
    > Justin Collins wrote:
    >
    >> Joe Ruby wrote:
    >>
    >>> Logan Capaldo wrote:
    >>>
    >>>
    >>>> some_exception_raising_call rescue nil # This is evil
    >>>>
    >>>>
    >>> Why is that evil?
    >>>
    >>> Joe
    >>>
    >>>

    >> Because if something goes wrong in that call, you will get no
    >> information about it. Won't even know it happened.
    >>
    >> -Justin
    >>

    >
    > Yeah, but rescue nil is just usually to deal with null values from the
    > database, for example, which isn't a big deal.
    >
    > Jo


    Using rescue this way will rescue all exceptions, but simply return nil.
    Examples:

    irb(main):001:0> puts a rescue nil
    => nil
    irb(main):002:0> puts "asiodhasd".asdiajsd rescue nil
    => nil
    irb(main):003:0> asdi.asdiojas.asidjas.asdihja.asdijasd rescue nil
    => nil
    irb(main):004:0>

    It's better, as I think Logan was implying, to rescue a specific
    exception if you are expecting it. That way, you will see if other
    exceptions occur.

    Sorry if I misunderstood your reply.

    -Justin
     
    Justin Collins, Aug 16, 2006
    #8
  9. Joe Ruby

    Joe Ruby Guest

    Aaahhh...all this time I thought I was rescuing nil rather than rescuing
    everything and returning nil!

    Joe

    --
    Posted via http://www.ruby-forum.com/.
     
    Joe Ruby, Aug 16, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Lionheart
    Replies:
    15
    Views:
    213
    Richard Lionheart
    Jul 18, 2004
  2. Harold Hausman

    Question about begin, rescue, end.

    Harold Hausman, Nov 18, 2005, in forum: Ruby
    Replies:
    9
    Views:
    198
    Stefan Kaes
    Nov 18, 2005
  3. S Kanakakorn

    Help begin/rescue/ensure

    S Kanakakorn, Feb 9, 2007, in forum: Ruby
    Replies:
    4
    Views:
    146
    Brian Candler
    Feb 9, 2007
  4. J. Cooper
    Replies:
    3
    Views:
    131
    J. Cooper
    Apr 1, 2008
  5. Iñaki Baz Castillo

    How to use "retry" into two deep begin/rescue/end ?

    Iñaki Baz Castillo, Apr 3, 2008, in forum: Ruby
    Replies:
    4
    Views:
    111
    Iñaki Baz Castillo
    Apr 3, 2008
Loading...

Share This Page