Rescue block doesn't get run

This code snippet processes the options correctly, but if I leave off a
required argument, it fails run my rescue block.

Why is that?

begin
opts = GetoptLong.new(
[ “–in”, “-f”, GetoptLong::REQUIRED_ARGUMENT ],
[ “–out”, “-o”, GetoptLong::REQUIRED_ARGUMENT ],
[ “–version”, GetoptLong::NO_ARGUMENT ],
[ “–help”, “-h”, “-?”, GetoptLong::NO_ARGUMENT ]
)

rescue GetoptLong::MissingArgument
$stderr.print "Missing Argument"
raise
end

···


Let us find clean and cheerful friends.
http://www.hacksaw.orghttp://www.privatecircus.com – KB1FVD

Hi,

···

In message “rescue block doesn’t get run” on 03/12/16, Hacksaw hacksaw@hacksaw.org writes:

This code snippet processes the options correctly, but if I leave off a
required argument, it fails run my rescue block.

Why is that?

Perhaps you forgot to call GetoptLong#get or #each.

						matz.

/tmp > cat foo.rb
begin
opts = GetoptLong.new(
[ “–in”, “-f”, GetoptLong::REQUIRED_ARGUMENT ],
[ “–out”, “-o”, GetoptLong::REQUIRED_ARGUMENT ],
[ “–version”, GetoptLong::NO_ARGUMENT ],
[ “–help”, “-h”, “-?”, GetoptLong::NO_ARGUMENT ]
)

rescue Exception => e
STDERR.printf “%s\n(%s)\n%s\n”, e.class, e, e.backtrace.join(“\n”)
end

/tmp > ruby foo.rb
NameError
(uninitialized constant GetoptLong)
foo.rb:2

/tmp > ruby -rgetoptlong foo.rb

because the error thrown is a NameError and you catch only
GetoptLong::MissingArgument. you just need to add:

require ‘getoptlong’

to your script! :wink:

-a

···

On Tue, 16 Dec 2003, Hacksaw wrote:

Date: Tue, 16 Dec 2003 22:48:11 +0900
From: Hacksaw hacksaw@hacksaw.org
Newsgroups: comp.lang.ruby
Subject: rescue block doesn’t get run

This code snippet processes the options correctly, but if I leave off a
required argument, it fails run my rescue block.

Why is that?

begin
opts = GetoptLong.new(
[ “–in”, “-f”, GetoptLong::REQUIRED_ARGUMENT ],
[ “–out”, “-o”, GetoptLong::REQUIRED_ARGUMENT ],
[ “–version”, GetoptLong::NO_ARGUMENT ],
[ “–help”, “-h”, “-?”, GetoptLong::NO_ARGUMENT ]
)

rescue GetoptLong::MissingArgument
$stderr.print “Missing Argument”
raise
end

ATTN: please update your address books with address below!

===============================================================================

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
STP :: Solar-Terrestrial Physics Data | NCEI
NGDC :: http://www.ngdc.noaa.gov/
NESDIS :: http://www.nesdis.noaa.gov/
NOAA :: http://www.noaa.gov/
US DOC :: http://www.commerce.gov/

The difference between art and science is that science is what we
understand well enough to explain to a computer.
Art is everything else.
– Donald Knuth, “Discover”

/bin/sh -c ‘for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done’
===============================================================================

Hi,

This code snippet processes the options correctly, but if I leave off a
required argument, it fails run my rescue block.
(snip)
rescue GetoptLong::MissingArgument
$stderr.print “Missing Argument”
raise

Don’t you re-raise the exception?

···

At Tue, 16 Dec 2003 22:48:11 +0900, Hacksaw wrote:


Nobu Nakada

Perhaps you forgot to call GetoptLong#get or #each.

  					matz.

Ahh, so you do late execution? I didn’t notice that it was complaining about
the opts.each line, below the section I quoted.

So I moved the rescue down to include the block which processes the options,
and all is happy.

Thanks. :slight_smile:

···


The performer can hide nothing, even the attempt to hide.
http://www.hacksaw.orghttp://www.privatecircus.com – KB1FVD