rescue and continue on next statement ?

Discussion in 'Ruby' started by Zouplaz, Dec 7, 2006.

  1. Zouplaz

    Zouplaz Guest

    Hello, I've seen there is a retry statement that rerun the entire
    begin/end block which raised the exception...

    I wonder if there is a way to 'continue' (from the rescue block) with
    the next statement following the one which raised the exception.

    Eg :

    begin
    File.delete("#{@fichier_destination_sql}.gz") # sql
    File.delete("#{@fichier_destination_tar}.tgz") # tar
    File.delete("/tmp/aboulafia-db-testdb_rbackup-uid1.gz")
    File.delete("/tmp/aboulafia-rep-ror_anaema-uid1.tgz")
    rescue

    end


    I want to avoid FileTest.
    If a file does not exists, the others will not be deleted which is not
    what I want.

    I would like

    begin
    # snip
    rescue
    continue
    end


    Is there a solution ? Or a common pattern to avoid this situation ?

    Thanks !
     
    Zouplaz, Dec 7, 2006
    #1
    1. Advertising

  2. Zouplaz

    Bira Guest

    On 12/7/06, Zouplaz <> wrote:

    > Is there a solution ? Or a common pattern to avoid this situation ?


    How about this?

    file_names = [/*your file names here*/]

    file_names.each do |file|

    begin
    File.delete(file)
    rescue
    /*Exception handling goes here */
    end

    end



    --
    Bira
    http://compexplicita.blogspot.com
    http://sinfoniaferida.blogspot.com
     
    Bira, Dec 7, 2006
    #2
    1. Advertising

  3. Zouplaz

    Eric Hodel Guest

    On Dec 7, 2006, at 04:10 , Zouplaz wrote:

    > Hello, I've seen there is a retry statement that rerun the entire
    > begin/end block which raised the exception...
    >
    > I wonder if there is a way to 'continue' (from the rescue block)
    > with the next statement following the one which raised the exception.


    File.delete "#{@fichier_destination_sql}.gz" rescue nil
    File.delete "#{@fichier_destination_tar}.tgz" rescue nil
    File.delete "/tmp/aboulafia-db-testdb_rbackup-uid1.gz" rescue nil
    File.delete "/tmp/aboulafia-rep-ror_anaema-uid1.tgz" rescue nil


    --
    Eric Hodel - - http://blog.segment7.net

    I LIT YOUR GEM ON FIRE!
     
    Eric Hodel, Dec 7, 2006
    #3
  4. Zouplaz

    Edwin Fine Guest

    Eric Hodel wrote:
    > On Dec 7, 2006, at 04:10 , Zouplaz wrote:
    >
    >> Hello, I've seen there is a retry statement that rerun the entire
    >> begin/end block which raised the exception...
    >>
    >> I wonder if there is a way to 'continue' (from the rescue block)
    >> with the next statement following the one which raised the exception.

    >
    > File.delete "#{@fichier_destination_sql}.gz" rescue nil
    > File.delete "#{@fichier_destination_tar}.tgz" rescue nil
    > File.delete "/tmp/aboulafia-db-testdb_rbackup-uid1.gz" rescue nil
    > File.delete "/tmp/aboulafia-rep-ror_anaema-uid1.tgz" rescue nil
    >
    >
    > --
    > Eric Hodel - - http://blog.segment7.net
    >
    > I LIT YOUR GEM ON FIRE!


    Hmmm... is it just because this is an example that the code is repeated?
    If not, how about something like

    %W[#{@fichier_destination_sql}.gz
    #{@fichier_destination_tar}.tgz
    /tmp/aboulafia-db-testdb_rbackup-uid1.gz"
    /tmp/aboulafia-rep-ror_anaema-uid1.tgz].each do |f|
    File.delete(f) rescue nil
    end

    --
    Posted via http://www.ruby-forum.com/.
     
    Edwin Fine, Dec 8, 2006
    #4
  5. Zouplaz

    Edwin Fine Guest

    Sorry, stray quote. I meant:

    %W[#{@fichier_destination_sql}.gz
    #{@fichier_destination_tar}.tgz
    /tmp/aboulafia-db-testdb_rbackup-uid1.gz
    /tmp/aboulafia-rep-ror_anaema-uid1.tgz].each do |f|
    File.delete(f) rescue nil
    end

    --
    Posted via http://www.ruby-forum.com/.
     
    Edwin Fine, Dec 8, 2006
    #5
  6. Zouplaz

    Juan Matias Guest

    Zouplaz wrote:
    > Hello, I've seen there is a retry statement that rerun the entire
    > begin/end block which raised the exception...
    >
    > I wonder if there is a way to 'continue' (from the rescue block) with
    > the next statement following the one which raised the exception.


    I try this in the "script/console" of my project and works:

    ["telefono","escritorio","sillones","sillas","sillones","cajas","modulares","archivos"].each
    do |title|
    begin
    p = Product.new:)title => title)
    p.save!
    rescue ActiveRecord::RecordInvalid => e
    #maybe collect errors
    next
    end
    end

    Juan Matias

    --
    Posted via http://www.ruby-forum.com/.
     
    Juan Matias, Jul 4, 2007
    #6
    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. Deniz Bahar
    Replies:
    2
    Views:
    503
    Andrey Tarasevich
    Mar 9, 2005
  2. dancer

    Continue code on next line

    dancer, May 8, 2007, in forum: ASP .Net
    Replies:
    7
    Views:
    13,640
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    May 9, 2007
  3. Joe Ruby

    begin...rescue...retry next?

    Joe Ruby, Aug 16, 2006, in forum: Ruby
    Replies:
    8
    Views:
    226
    Joe Ruby
    Aug 16, 2006
  4. Karthi kn
    Replies:
    2
    Views:
    379
  5. RichardOnRails
    Replies:
    4
    Views:
    200
    RichardOnRails
    Sep 27, 2010
Loading...

Share This Page