OptionParser and Exceptions

S

Srijayanth Sridhar

[Note: parts of this message were removed to make it a legal post.]

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
 
J

Jan Friedrich

Srijayanth Sridhar said:
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
 
R

Robert Klemme

2009/4/15 Srijayanth Sridhar said:
Hello,

The following code:

require 'optparse'

optparser =3D OptionParser.new do |args|
=A0 =A0 =A0 =A0list=3D[:foo,:bar,:baz]
=A0 =A0 =A0 =A0begin
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0args.on("--l [OPTION]",list,"Blah") { |o| = }
=A0 =A0 =A0 =A0rescue
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0puts "Rescuing in the block"
=A0 =A0 =A0 =A0end

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
=A0 =A0 =A0 =A0optparser.parse!(ARGV)
rescue
=A0 =A0 =A0 =A0puts "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 al= l,
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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
S

Srijayanth Sridhar

[Note: parts of this message were removed to make it a legal post.]

Thanks both of you.

Jayanth

2009/4/15 Srijayanth Sridhar said:
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
 
B

Brian Candler

Robert said:
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.
 
R

Robert Klemme

2009/4/15 Brian Candler said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top