From: "Ara.T.Howard" Date: 2003-12-17T02:26:59+09:00 Subject: Re: rescue block doesn't get run On Tue, 16 Dec 2003, Hacksaw wrote: > Date: Tue, 16 Dec 2003 22:48:11 +0900 > From: Hacksaw > 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' ===============================================================================