From: Robert Klemme Date: 2010-07-28T20:10:07+09:00 Subject: Re: Accessing object in rescue block On 28.07.2010 10:54, Florian Odronitz wrote: > when I rescue an exception, is there a way to access the object where > the exception was raised? > > For example: > > begin > "something".nonexistent > rescue NoMethodError => e > # here > end > > In the rescue block, can I access the String "something"? There is no way I am aware of which allows for fetching the instance from the NoMethodError if this is what you want. If you display the exception you will see a textual description probably derived from #inspect: irb(main):001:0> begin irb(main):002:1* "".foo irb(main):003:1> rescue Exception => e irb(main):004:1> puts e.display irb(main):005:1> end undefined method `foo' for "":String => nil irb(main):006:0> If that is not sufficient for you, you can reference a variable defined after "begin": irb(main):006:0> begin irb(main):007:1* x = "" irb(main):008:1> x.foo irb(main):009:1> rescue Exception => e irb(main):010:1> p x irb(main):011:1> end "" => "" irb(main):012:0> Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/