[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Context of an Error

Trans

10/4/2006 1:46:00 PM

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.

4 Answers

Ara.T.Howard

10/4/2006 2:32:00 PM

0

Logan Capaldo

10/5/2006 12:19:00 AM

0

On Wed, Oct 04, 2006 at 10:50:05PM +0900, Trans wrote:
> 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.
>
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.


Rick DeNatale

10/5/2006 1:28:00 PM

0

On 10/4/06, Logan Capaldo <logancapaldo@gmail.com> 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.denh...

Logan Capaldo

10/5/2006 1:42:00 PM

0

On Thu, Oct 05, 2006 at 10:27:33PM +0900, Rick DeNatale wrote:
> On 10/4/06, Logan Capaldo <logancapaldo@gmail.com> 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
>
>
> >Totally untested of course.
>
> ditto
>
>
> --
> Rick DeNatale
>
> My blog on Ruby
> http://talklikeaduck.denh...