From: Srijayanth Sridhar Date: 2009-04-15T21:14:42+09:00 Subject: Re: OptionParser and Exceptions --001636e0b473607073046796e474 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Thanks both of you. Jayanth On Wed, Apr 15, 2009 at 5:35 PM, Robert Klemme wrote: > 2009/4/15 Srijayanth Sridhar : > > 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 > > This rescue does not make sense. Because the code is invoked once > upon OptionParser.new. Parsing errors are thrown from parse and > parse!. > > > 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. > > 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 > > -- > remember.guy do |as, often| as.you_can - without end > http://blog.rubybestpractices.com/ > > --001636e0b473607073046796e474--