Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > rescue and continue on next statement ?

Reply
Thread Tools

rescue and continue on next statement ?

 
 
Zouplaz
Guest
Posts: n/a
 
      12-07-2006
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 !
 
Reply With Quote
 
 
 
 
Bira
Guest
Posts: n/a
 
      12-07-2006
On 12/7/06, Zouplaz <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Eric Hodel
Guest
Posts: n/a
 
      12-07-2006
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://www.velocityreviews.com/forums/(E-Mail Removed) - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!


 
Reply With Quote
 
Edwin Fine
Guest
Posts: n/a
 
      12-08-2006
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 - (E-Mail Removed) - 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/.

 
Reply With Quote
 
Edwin Fine
Guest
Posts: n/a
 
      12-08-2006
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/.

 
Reply With Quote
 
Juan Matias
Guest
Posts: n/a
 
      07-04-2007
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","sillo nes","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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ruby exception statement not suppressed within begin-rescue-end block RichardOnRails Ruby 4 09-27-2010 07:15 PM
Problem in exiting from a file and continue with next file Karthi kn Ruby 2 04-10-2008 01:44 PM
Continue code on next line dancer ASP .Net 7 05-09-2007 01:22 AM
begin...rescue...retry next? Joe Ruby Ruby 8 08-16-2006 07:58 PM
CurrentElement->next = CurrentElement->next->next (UNDEFINED?) Deniz Bahar C Programming 2 03-09-2005 12:45 AM



Advertisments