Forum: Ruby Nested Exceptions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Joe S. (Guest)
on 2006-05-18 19:24
Can anyone tell me if Ruby supports nested exceptions correctly? I have
tried finding documentation on this issue, but have come up emtpy
handed.  What I want to do is something along the lines of this.

begin
    Some common setup code that could cause an exception goes here
    ...
    ...
    for i in 0..x
        begin
            # Other exceptions may occur within the loop.
        rescue
            # If an exception occurs within the loop I want to handle
the
            #  exception and continue with the loop.
        end
    end
end

thanks
-joe
Guillaume M. (Guest)
on 2006-05-18 19:56
(Received via mailing list)
Does this answer your question?

[gus@gusmac gus]$ cat tmp/exception.rb
#! /usr/bin/ruby

begin
   rand > 0.5 and raise "Hi from Murphy!"
   10.times do |i|
     begin
       i%2 == 0 and raise "Yack, #{i} is even"
     rescue => e
       puts("In loop exception: #{e.message}")
     end
   end
rescue => e
   puts("Setup failed: #{e.message}")
end
[gus@gusmac gus]$ ruby tmp/exception.rb
Setup failed: Hi from Murphy!
[gus@gusmac gus]$ ruby tmp/exception.rb
In loop exception: Yack, 0 is even
In loop exception: Yack, 2 is even
In loop exception: Yack, 4 is even
In loop exception: Yack, 6 is even
In loop exception: Yack, 8 is even

Guillaume.

Le 18 mai 06, à 11:24, Joe S. a écrit :
This topic is locked and can not be replied to.