How to catch any exception "eval" throws

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”?

this is from the book:

eval( aString [, aBinding [ file [ line ] ] ]) -> anObject

i have no guess how to use this. what do the nested [] mean?

thanks for help, -Meinrad

eval( aString [, aBinding [ file [ line ] ] ]) → anObject

i have no guess how to use this. what do the nested mean?

everything beetween sqaure brackets means “optional”.

Nested brackets mean: the inner depends on outer
this means you can use:

eval (“puts b”)
eval (“puts b”, binding)
eval (“puts b”, binding,myfile)
eval (“puts b”, binding,myfile, 20)

the “->” thing represents the output of the method.
In this case you get an instance of class Object (an Object)

···

il Mon, 31 Mar 2003 00:18:33 +0200, Meinrad Recheis meinrad.recheis@aon.at ha scritto::

thanks for help, -Meinrad

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”?

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.

this is from the book:

eval( aString [, aBinding [ file [ line ] ] ]) → anObject

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

~ > cat foo.rb
#!/usr/bin/env ruby

%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>

eval(;|;|) caused
#<SyntaxError: (eval):1: compile error
(eval):1: parse error>

eval(raise(‘badthings’)) caused
#<RuntimeError: (eval):1: badthings>

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
====================================

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
=> #<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:

On Mon, 31 Mar 2003, Meinrad Recheis wrote:


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
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