rescue block doesn't get run

H

Hacksaw

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
 
A

Ara.T.Howard

Date: Tue, 16 Dec 2003 22:48:11 +0900
From: Hacksaw <[email protected]>
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

/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! ;-)

-a
--

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 :: http://www.ngdc.noaa.gov/stp/
| 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'
===============================================================================
 
Y

Yukihiro Matsumoto

Hi,

In message "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?

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

matz.
 
N

nobu.nokada

Hi,

At Tue, 16 Dec 2003 22:48:11 +0900,
Hacksaw said:
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?
 

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,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top