# Accessing object in rescue block

**URL:** <https://www.ruby-forum.com/t/accessing-object-in-rescue-block/192326>\
**Category:** Ruby\
**Created:** [July 28, 2010, 10:54am UTC](https://www.ruby-forum.com/t/accessing-object-in-rescue-block/192326 "2010-07-28T10:54:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![odo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@odo](https://www.ruby-forum.com/u/odo)\
**Post date:** [July 28, 2010, 10:54am UTC](https://www.ruby-forum.com/t/accessing-object-in-rescue-block/192326/1 "2010-07-28T10:54:07Z")

</div>

Hi,

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”?

Best,  
Florian

---

<div class="post-metadata">

**Author:** ![odo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@odo](https://www.ruby-forum.com/u/odo)\
**Post date:** [July 28, 2010, 1:11pm UTC](https://www.ruby-forum.com/t/accessing-object-in-rescue-block/192326/2 "2010-07-28T13:11:00Z")

</div>

On 28.07.2010 10:54, Florian O. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![odo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@odo](https://www.ruby-forum.com/u/odo)\
**Post date:** [July 28, 2010, 1:01pm UTC](https://www.ruby-forum.com/t/accessing-object-in-rescue-block/192326/3 "2010-07-28T13:01:02Z")

</div>

On 28 Juli, 10:54, Florian O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # here
> 
> end
> 
> In the rescue block, can I access the String “something”?
> 
> ## Best, Florian
> 
> Posted viahttp://www.ruby-forum.com/.

Not by default. According to  
[http://www.ruby-doc.org/ruby-1.9/classes/Exception.html](http://www.ruby-doc.org/ruby-1.9/classes/Exception.html)  
an exception provides “…information about the exception—its type  
(the exception’s class name), an optional descriptive string, and  
optional traceback information…”

However the same page also says “…Programs may subclass Exception to  
add additional information.”

So, if your roll your own exceptions it’s possible to add this  
functionality.

class MyException \< Exception

attr\_accessor :source

def initialize(msg = nil, source = nil)  
super(msg)  
@source = source  
end  
end

class Test  
def enclose(obj)  
if obj.kind\_of?(String)  
“(#{obj})”  
else  
raise MyException.new(‘Enclose needs a string!’, obj)  
end  
end  
end

begin  
t = Test.new  
puts t.enclose(‘foo’)  
puts t.enclose(5)  
rescue MyException =\> err  
puts “Error: #{err.message}”  
puts “Source: #{err.source}”  
end

Overriding the default Exception class might also be possible (after  
all, it’s ruby) but changing “default” behavior can be pretty risky as  
well.

/lasso

---

<div class="post-metadata">

**Author:** ![odo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@odo](https://www.ruby-forum.com/u/odo)\
**Post date:** [July 30, 2010, 10:52am UTC](https://www.ruby-forum.com/t/accessing-object-in-rescue-block/192326/4 "2010-07-30T10:52:48Z")

</div>

Thanks for your thoughts, I figured it out.

I was coming from this Article:

> **[J-\_-L | The 28 Bytes of Ruby Joy!](https://rbjl.janlelis.com/26-the-28-bytes-of-ruby-joy)**
>
> Working with Ruby

The author suggests to let the method\_missing of Nil always return nil  
so you could do things like:  
some\_object.this\_is\_null.do\_something\_more  
would return nil and not raise on the undefined method  
‘do\_something\_more’ on nil.  
I thought such an approach was to intrusive and it would be better to do  
something like:

ignore\_nil{some\_object.this\_is\_null.do\_something\_more}

My current implementation looks like this:

class NoMethodErrorInNil \< NoMethodError; end

class NilClass  
def method\_missing(method\_name, \*args)  
raise NoMethodErrorInNil, “undefined method `#{method\_name}’ for  
nil:NilClass”  
end  
end

class Object  
def ignore\_nil(return\_value = nil, &block)  
begin  
yield  
rescue NoMethodErrorInNil =\> e  
return return\_value  
end  
end  
end
