[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Adding Information to Exceptions

Tim Hunter

7/15/2003 12:32:00 PM

What am I doing wrong here? This little test program is based on the
example on p. 97 in the Pickaxe:

class MyError < StandardError
def initialize(loc)
@extra = loc
end
def extra
@extra
end
end

def read_file(name)
raise MyError.new("line 1"), "Unable to read file: #{name}"
end

begin
read_file('foo.jpg')
rescue MyError => error
puts "error=#{error}"
puts "error.extra=#{error.extra}"
end

I expect the 2nd puts to print "line 1". However, the output is:

[tim:~]$ ruby test.rb
error=MyError
error.extra=Unable to read file: foo.jpg

This is Ruby 1.6.8.
2 Answers

ts

7/15/2003 1:37:00 PM

0

>>>>> "T" == Tim Hunter <cyclists@nc.rr.com> writes:

T> This is Ruby 1.6.8.

Well, for 1.6.8 see [ruby_talk:36408]

http://www.ruby_talk...


Guy Decoux


nobu.nokada

7/15/2003 8:24:00 PM

0

Hi,

At Tue, 15 Jul 2003 22:17:11 +0900,
Tim Hunter wrote:
> def read_file(name)
> raise MyError.new("line 1"), "Unable to read file: #{name}"

Kernel#raise calls #exception method of the first argument with
the rest, and throws the result.

In 1.6, Exception#exception creates new instance of the class
of self, but in 1.8 just sets mesg and backtrace to a clone.

revision 1.34
date: 2001/11/13 08:19:51; author: matz; state: Exp; lines: +2 -2
* error.c (exc_exception): set "mesg" directly to the clone. it
might be better to set mesg via some method for flexibility.

--
Nobu Nakada