A Syntax question

Hey Guys,

I would have thought the following two code snippets were equivalent.
However, only the first one will raise err. Can someone explain to me
how I should be ‘reading’ the 2nd snippet?

  rescue Exception => err
    begin
        RDoc::usage
    rescue SystemExit
    end
    raise err


  rescue Exception => err
    begin RDoc::usage rescue SystemExit end
    raise err

Sonny.

On Apr 4, 1:01 pm, Sonny C. [email protected] wrote:

    end
    raise err

  rescue Exception => err
    begin RDoc::usage rescue SystemExit end
    raise err

Sonny.


Posted viahttp://www.ruby-forum.com/.

This may be wrong, but this works:

begin
raise err
rescue Exception => err
begin
puts “bummer 1”
raise err2
rescue Exception => err2
puts “bummer 1.1”
end
end

.vs.

begin
raise err
rescue Exception => err
begin puts “bummer 2”; raise err2; rescue Exception =>err2; puts
“bummer 2.1”; end
end

Thanks Dale. I’m not actually looking for a workaround. Snippet #1 of
my original post does work. I’m trying to understand why snippet #2
doesn’t.

The only difference between the two are the whitespaces. If someone
could help me understand how snippet #2 is being parsed/read by the
interpretter that would be super.

Awesome. That clarifies things. Thanks Brian.

On Thu, Apr 05, 2007 at 03:01:56AM +0900, Sonny C. wrote:

    rescue SystemExit
    end
    raise err


  rescue Exception => err
    begin RDoc::usage rescue SystemExit end
    raise err

These are two different syntactic constructions.

   <expression1> rescue <expression2>

returns the value of , unless there is an exception in
which
case it returns the value of

In this construct, you cannot specify the exception class. I think it
only
catches StandardError and subclasses.

Typical usage:

 foo rescue nil                 #  just ignore StandardError

 a = foo rescue bar rescue baz  #  return value from foo, if 

exception
# then bar, if exception then baz

Regards,

Brian.