but my program terminates if str contains erraneous expressions
why doesn t rescue catch say “parser errors”?
I can never remember exactly how this works,
but the default is to catch a certain family
of errors (perhaps StandardError? I’m probably
misleading you).
Putting a very general class there would help,
perhaps ‘rescue Exception’ – but I’m not sure
that is correct.
i have no guess how to use this. what do the nested mean?
That simply means that the parameters are
independently optional. These are all
valid:
eval(a,b,f,l)
eval(a,b,f)
eval(a,b
eval(a)
HTH,
Hal
···
----- Original Message -----
From: “Meinrad Recheis” meinrad.recheis@aon.at
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Sunday, March 30, 2003 4:31 PM
Subject: how to catch any exception “eval” throws
%w[ error ;|;| raise(‘badthings’) ].each do |badthing|
begin
eval badthing
rescue NameError, SyntaxError, StandardError => err
print “eval(#{badthing}) caused\n\t”
p err
print “\n”
end
end
~> ./foo.rb
eval(error) caused
#<NameError: (eval):1: undefined local variable or method `error’ for #Object:0x40190ce0>
there are probably more to catch - but this is the bulk of them.
-a
···
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”?
–
====================================
Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================
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
=> #<A:0x40207fbc>
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.
···
On Mon, Mar 31, 2003 at 03:13:15PM +0900, ahoward wrote: