# OptionParser and Exceptions

**URL:** <https://www.ruby-forum.com/t/optionparser-and-exceptions/165581>\
**Category:** Ruby\
**Created:** [April 15, 2009, 1:47pm UTC](https://www.ruby-forum.com/t/optionparser-and-exceptions/165581 "2009-04-15T13:47:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [April 15, 2009, 1:47pm UTC](https://www.ruby-forum.com/t/optionparser-and-exceptions/165581/1 "2009-04-15T13:47:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [April 15, 2009, 2:01pm UTC](https://www.ruby-forum.com/t/optionparser-and-exceptions/165581/2 "2009-04-15T14:01:36Z")

</div>

Srijayanth S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [April 15, 2009, 2:15pm UTC](https://www.ruby-forum.com/t/optionparser-and-exceptions/165581/3 "2009-04-15T14:15:40Z")

</div>

Thanks both of you.

Jayanth

On Wed, Apr 15, 2009 at 5:35 PM, Robert K.

---

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [April 15, 2009, 2:58pm UTC](https://www.ruby-forum.com/t/optionparser-and-exceptions/165581/4 "2009-04-15T14:58:00Z")

</div>

Robert K. 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:

> **[Ruby | zenspider.com | by ryan davis](https://www.zenspider.com/ruby/)**
>
> Ruby facts, opinions, source code, links, and stuff.

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.

---

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [April 15, 2009, 2:06pm UTC](https://www.ruby-forum.com/t/optionparser-and-exceptions/165581/5 "2009-04-15T14:06:19Z")

</div>

2009/4/15 Srijayanth S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> 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

---

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [April 15, 2009, 3:12pm UTC](https://www.ruby-forum.com/t/optionparser-and-exceptions/165581/6 "2009-04-15T15:12:16Z")

</div>

2009/4/15 Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Robert K. 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
