Forum: Ruby OptionParser and 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.
1e736afdcdfe0753d67a81d449aca590?d=identicon&s=25 Srijayanth Sridhar (Guest)
on 2009-04-15 13:47
(Received via mailing list)
Hello,

The following code:

require 'optparse'

optparser = OptionParser.new do |args|
        list=[:foo,:bar,:baz]
        begin
                args.on("--l [OPTION]",list,"Blah") { |o| }
        rescue
                puts "Rescuing in the block"
        end
end

begin
        optparser.parse!(ARGV)
rescue
        puts "Rescuing in main"
end

./foo.rb -l FOO ends up being rescued in main.

Why is this? the parse! method just calls the block defined in new
doesn't
it? In which case, a begin and rescue block should work there right?
Even if
I remove the begin and rescue block in main, it doesn't get rescued at
all,
it just ends up spitting the usual ugly error etc.

I am currently on Ruby 1.8.6.

Thanks,

Jayanth
B32a1911d783e0a95e0b062fd5d0b64b?d=identicon&s=25 Jan Friedrich (janfri)
on 2009-04-15 14:01
(Received via mailing list)
Srijayanth Sridhar <srijayanth@gmail.com> wrote:
> Why is this? the parse! method just calls the block defined in new
> doesn't it?
No it doesn't:

require 'optparse'

optparser = OptionParser.new do |args|
  puts 'Here in OptionParser.new'
end

puts 'Before optparser.parse!'
optparser.parse!
puts 'After  optparser.parse!'

Regards,
Jan
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-04-15 14:06
(Received via mailing list)
2009/4/15 Srijayanth Sridhar <srijayanth@gmail.com>:
>        rescue
>                puts "Rescuing in the block"
>        end

This rescue does not make sense.  Because the code is invoked once
upon OptionParser.new.  Parsing errors are thrown from parse and
parse!.

> Why is this? the parse! method just calls the block defined in new doesn't
> it? In which case, a begin and rescue block should work there right? Even if
> I remove the begin and rescue block in main, it doesn't get rescued at all,
> it just ends up spitting the usual ugly error etc.

You need to rescue Exception because rescue without any arguments only
rescues RuntimeErrors IIRC, it definitively does not catch every sub
class of Exception.

Cheers

robert
1e736afdcdfe0753d67a81d449aca590?d=identicon&s=25 Srijayanth Sridhar (Guest)
on 2009-04-15 14:15
(Received via mailing list)
Thanks both of you.

Jayanth

On Wed, Apr 15, 2009 at 5:35 PM, Robert Klemme
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-04-15 14:58
Robert Klemme wrote:
> You need to rescue Exception because rescue without any arguments only
> rescues RuntimeErrors IIRC

rescue without any arguments rescues StandardError. (StandardError is an
ancestor of RuntimeError, and Exception is an ancestor of StandardError)

See:
http://www.zenspider.com/Languages/Ruby/QuickRef.html#34

You should normally be a bit wary of 'rescue Exception'. This will
rescue all sorts of internal failures like NoMemoryError or SyntaxError
which you may not want to hide.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-04-15 15:12
(Received via mailing list)
2009/4/15 Brian Candler <b.candler@pobox.com>:
> Robert Klemme wrote:
>> You need to rescue Exception because rescue without any arguments only
>> rescues RuntimeErrors IIRC
>
> rescue without any arguments rescues StandardError. (StandardError is an
> ancestor of RuntimeError, and Exception is an ancestor of StandardError)

Thank you for the correction!

robert
This topic is locked and can not be replied to.