# Context of an Error

**URL:** <https://www.ruby-forum.com/t/context-of-an-error/70985>\
**Category:** Ruby\
**Created:** [October 4, 2006, 3:50pm UTC](https://www.ruby-forum.com/t/context-of-an-error/70985 "2006-10-04T15:50:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [October 4, 2006, 3:50pm UTC](https://www.ruby-forum.com/t/context-of-an-error/70985/1 "2006-10-04T15:50:34Z")

</div>

I feel like I should know how to do this already, but… How do I get  
the context in which an error was thrown?

x = SomeClass

def dosomething  
x.bar  
end

begin  
dosomething  
rescue NoMethodError  
p e.context #=\> SomeClass (HOW?)  
end

Thanks,  
T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [October 4, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/context-of-an-error/70985/2 "2006-10-04T16:32:37Z")

</div>

On Wed, 4 Oct 2006, Trans wrote:

> dosomething  
> rescue NoMethodError  
> p e.context #=\> SomeClass (HOW?)  
> end
> 
> Thanks,  
> T.

but, imho, that’s not the context. the context would be ‘dosomething’ ?  
can  
you explain what you mean by ‘context’? if by context you mean the  
object  
raising the NoMethodError, you can do obviously hook into method\_missing  
to  
make it add this info to every execption, otherwise this is all you’ve  
got:

harp:~ \> cat a.rb  
class C; end

begin  
C.foobar  
rescue NoMethodError =\> ne  
(NoMethodError.instance\_methods -  
Object.instance\_methods).sort.each do |m|  
p m =\> ne.send(m)  
end  
end

harp:~ \> ruby a.rb  
{“args”=\>[]}  
{“backtrace”=\>[“a.rb:4”]}  
{“exception”=\>#\<NoMethodError: undefined method `foobar' for C:Class>} {"message"=>"undefined method`foobar’ for C:Class"}  
{“name”=\>:foobar}  
a.rb:7:in `set\_backtrace’: wrong number of arguments (0 for 1)  
(ArgumentError)  
from a.rb:7  
from a.rb:6

so, other than scraping that info - which does contain what you want - i  
think  
adding an attr to Exception and making method\_missing set it on the way  
out  
might be the only way to go…

cheers.

-a

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [October 5, 2006, 2:19am UTC](https://www.ruby-forum.com/t/context-of-an-error/70985/3 "2006-10-05T02:19:34Z")

</div>

On Wed, Oct 04, 2006 at 10:50:05PM +0900, Trans wrote:

> ```
> dosomething
> 
> ```
> 
> rescue NoMethodError  
> p e.context #=\> SomeClass (HOW?)  
> end
> 
> Thanks,  
> T.

I don’t know if there is a builtin way but:  
class Object  
alias lmc\_ruby\_raise raise  
def raise(\*args)  
if args.length \>= 2  
# class + some args  
klass, \*rem\_args = args  
exp = klass.new(\*rem\_args)  
exp.instance\_variable\_set("@context", self.class)  
lmc\_ruby\_raise(exp)  
elsif args.length == 1  
case args[0]  
when Class  
exp = args[0].new  
exp.instance\_variable\_set("@context", self.class)  
lmc\_ruby\_raise( exp )  
when String  
exp = RuntimeError.new(args[0])  
exp.instance\_variable\_set("@context", self.class)  
lmc\_ruby\_raise( exp )  
else  
exp = args[0]  
exp.instance\_variable\_set("@context", self.class)  
lmc\_ruby\_raise( exp )  
end  
end  
end  
end

class Exception  
attr\_reader :context  
end

Totally untested of course.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [October 5, 2006, 3:42pm UTC](https://www.ruby-forum.com/t/context-of-an-error/70985/4 "2006-10-05T15:42:51Z")

</div>

On Thu, Oct 05, 2006 at 10:27:33PM +0900, Rick DeNatale wrote:

> On 10/4/06, Logan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I don’t know if there is a builtin way but:  
> > class Object
> 
> Perhaps this should be  
> module Kernel
> 
> since that’s where raise actually lives.  
> Well actually if I put it in Object and raise is defined in Kernel that  
> would simplify my code. No need for an alias, just use super

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [October 5, 2006, 3:28pm UTC](https://www.ruby-forum.com/t/context-of-an-error/70985/5 "2006-10-05T15:28:48Z")

</div>

On 10/4/06, Logan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I don’t know if there is a builtin way but:  
> class Object

Perhaps this should be  
module Kernel

since that’s where raise actually lives.

> Totally untested of course.

ditto

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
