Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Adding Information to Exceptions

Reply
Thread Tools

Adding Information to Exceptions

 
 
Tim Hunter
Guest
Posts: n/a
 
      07-15-2003
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.
 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      07-15-2003
>>>>> "T" == Tim Hunter <> writes:

T> This is Ruby 1.6.8.

Well, for 1.6.8 see [ruby_talk:36408]

http://www.ruby_talk.org/36408


Guy Decoux


 
Reply With Quote
 
 
 
 
nobu.nokada@softhome.net
Guest
Posts: n/a
 
      07-15-2003
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exceptions - How do you make it work like built-in exceptions? Lie Python 3 01-14-2008 06:45 PM
Exceptions + Performance on path without exceptions gratch06@gmail.com C++ 3 04-16-2007 08:52 PM
where to find information about errors/exceptions in socket.py mirandacascade@yahoo.com Python 1 10-07-2005 07:36 AM
Checked exceptions vs unchecked exceptions Ahmed Moustafa Java 5 07-14-2004 01:46 PM
Custom exceptions -- inherit from exceptions.Exception? Paul Miller Python 3 11-12-2003 09:24 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57