Exception types

Hi,

Can anybody help me on the rescue clause? I tried to catch file not found
error of file.open method, but I don’t know what type of error to use in
rescue clause. i tried to write nothing after rescue, it seems not catching
the error. Further more:

  1. where can I find a reference to different kind of runtime errors? It
    seems that there are no information on that (Exception Class?) in pickaxe…

  2. in “rescue SyntaxError, NameError => boom”, is => used to denote a hash?
    if so, why NameError is a hash? If not, maybe gavin can add this usage of =>
    to the FunnySymbolsInCode FAQ…

Thanks,
Shannon

···

Tired of spam? Get advanced junk mail protection with MSN 8.
http://join.msn.com/?page=features/junkmail

Can anybody help me on the rescue clause? I tried to catch file not found
error of file.open method, but I don't know what type of error to use in
rescue clause. i tried to write nothing after rescue, it seems not catching
the error. Further more:

Well, what error ruby give you ?

pigeon% ls aa
ls: aa: No such file or directory
pigeon%

pigeon% ruby -e 'File.open("aa") rescue true'
pigeon%

1. where can I find a reference to different kind of runtime errors? It
seems that there are no information on that (Exception Class?) in pickaxe...

p. 303

2. in "rescue SyntaxError, NameError => boom", is => used to denote a hash?
if so, why NameError is a hash?

well,

   begin
      # do something
   rescue SyntaxError, NameError => boom
      # do something
   end

is the same than

   begin
      # do something
   rescue SyntaxError, NameError
      boom = $!
      # do something
   end

Guy Decoux

···

_________________________________________________________________
Tired of spam? Get advanced junk mail protection with MSN 8.
http://join.msn.com/?page=features/junkmail

Hi,

Can anybody help me on the rescue clause? I tried to catch file not found
error of file.open method, but I don’t know what type of error to use in
rescue clause. i tried to write nothing after rescue, it seems not catching
the error. Further more:

Shannon, each rescue needs the name of one or more Exception classes
to catch. I used irb to find out what exception File.open raises for
file not found:

irb(main):001:0> File.open("xxx.yyy")	
Errno::ENOENT: No such file or directory - "xxx.yyy"
	from (irb):1:in 'open'
	from (irb):1

Looks like Errno::ENOENT is the exception, so this works:

begin
File.open(“xxx.yyy”)
rescue Errno::ENOENT
puts “caught”
end

  1. where can I find a reference to different kind of runtime errors? It
    seems that there are no information on that (Exception Class?) in pickaxe…

IIRC (I don’t have my copy of Pickaxe beside me) there are two lists
of exceptions in the Pickaxe, one in the “Exceptions, Catch, and
Throw” chapter and one in the description of the Exception class in
the “Standard Library” chapter. I don’t see them in my online copy so
it may be that those lists are actually illustrations, which don’t
appear in the online version.

···

On Tue, 3 Dec 2002 22:49:59 +0900, “Shannon Fang” xrfang@hotmail.com wrote:

  1. in “rescue SyntaxError, NameError => boom”, is => used to denote a hash?
    if so, why NameError is a hash? If not, maybe gavin can add this usage of =>
    to the FunnySymbolsInCode FAQ…

Thanks,
Shannon


Tired of spam? Get advanced junk mail protection with MSN 8.
http://join.msn.com/?page=features/junkmail

Have you ever had pickaxe in your hands? Have you read it through at least
once? It has a big chapter dedicated to exceptions with a nice table of
exception inheritance tree. It has a very useful index too, by the way.

···

----- Original Message -----
From: “Shannon Fang” xrfang@hotmail.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, December 03, 2002 5:49 AM
Subject: exception types

Hi,

Can anybody help me on the rescue clause? I tried to catch file not found
error of file.open method, but I don’t know what type of error to use in
rescue clause. i tried to write nothing after rescue, it seems not
catching
the error. Further more:

  1. where can I find a reference to different kind of runtime errors? It
    seems that there are no information on that (Exception Class?) in
    pickaxe…

  2. in “rescue SyntaxError, NameError => boom”, is => used to denote a
    hash?
    if so, why NameError is a hash? If not, maybe gavin can add this usage of
    =>
    to the FunnySymbolsInCode FAQ…

Thanks,
Shannon


Tired of spam? Get advanced junk mail protection with MSN 8.
http://join.msn.com/?page=features/junkmail

Shannon Fang wrote:

  1. where can I find a reference to different kind of runtime errors? It
    seems that there are no information on that (Exception Class?) in
    pickaxe…

You can make it youself with Ruby! Here’s one way, from the examples
that come with the “enumerable tools” on RAA:

require ‘enum/tree’

subclasses = proc { |cl|
subs =
ObjectSpace.each_object(Class) { |sub|
if sub.superclass == cl then subs << sub end
}
subs
}

root = Object.const_get(ARGV[0])
for x, a in root.by_depth(subclasses).with_ancestors
print “\t”*a.size, x, “\n”
end

Then if you run this from the command line with the argument
‘Exception’, you get:

Exception
NoMemoryError
ScriptError
NotImplementedError
LoadError
SyntaxError
StandardError
SystemStackError
LocalJumpError
IOError
EOFError
RegexpError

  1. in “rescue SyntaxError, NameError => boom”, is => used to denote a hash?
    if so, why NameError is a hash? If not, maybe gavin can add this usage of =>
    to the FunnySymbolsInCode FAQ…

Good idea; done.

Gavin

···

From: “Shannon Fang” xrfang@hotmail.com