From: "Mauricio Fernández" Date: 2003-03-31T20:14:14+09:00 Subject: Re: how to catch any exception "eval" throws On Mon, Mar 31, 2003 at 03:13:15PM +0900, ahoward wrote: > On Mon, 31 Mar 2003, Meinrad Recheis wrote: > > > i tried > > > > begin > > eval( str ) > > rescue > > puts $! > > end > > > > but my program terminates if str contains erraneous expressions > > why doesn t rescue catch say "parser errors"? > > > ~ > cat foo.rb > #!/usr/bin/env ruby > > %w[ error ;|;| raise('badthings') ].each do |badthing| > begin > eval badthing > rescue NameError, SyntaxError, StandardError => err Exception should be enough unless somebody is knowingly raising other things... (it's at the top of the exception class hierarchy) [one minute later] It's actually impossible to raise something unrelated to Exception: >> class A; end => nil >> raise A TypeError: exception class/object expected from (irb):3:in `raise' from (irb):3 >> raise 1 TypeError: exception class/object expected from (irb):4:in `raise' from (irb):4 >> a = A.new => # >> class << a; def exception; A; end; end => nil >> raise a TypeError: exception object expected from (irb):10:in `raise' from (irb):10 begin .... rescue Exception ... end will thus catch everything. -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com Linux: Where Don't We Want To Go Today? -- Submitted by Pancrazio De Mauro, paraphrasing some well-known sales talk