[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

rescue and continue on next statement ?

user@domain.invalid

12/7/2006 12:09:00 PM

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 !
5 Answers

Bira

12/7/2006 12:52:00 PM

0

On 12/7/06, Zouplaz <user@domain.invalid> 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.bl...
http://sinfoniaferida.bl...

Eric Hodel

12/7/2006 10:34:00 PM

0

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 - drbrain@segment7.net - http://blog.se...

I LIT YOUR GEM ON FIRE!


Edwin Fine

12/8/2006 5:50:00 AM

0

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 - drbrain@segment7.net - http://blog.se...
>
> 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-....

Edwin Fine

12/8/2006 5:51:00 AM

0

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-....

Juan Matías

7/4/2007 10:25:00 PM

0

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-....