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

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

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

Interestingly, this works on 1.8, but not on 1.6.8. An approach that 
works for both is:


class MyError < StandardError
     def initialize(loc, msg)
         super(msg)
         @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


Cheers


Dave