rescue and continue on next statement ?

Z

Zouplaz

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 !
 
B

Bira

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
 
E

Eric Hodel

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
 
E

Edwin Fine

Eric said:
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

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
 
E

Edwin Fine

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
 
J

Juan Matias

Zouplaz said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top