Bindings at the time of Exceptions

(Adam Sanderson) #1

I always thought it would be neat to be able to get the binding at the
time an exception was raised so that one could see the state the stack
was in when an exception occurred.

It would be nice list the local variables and values at the time of the
exception for instance. Can anyone think of some trickery to do this?
I think it would be of great use for debugging and whatnot, and
massive points for cool ruby hackery :wink:
聽聽.adam sanderson

(Joel VanderWerf) #2

Adam Sanderson wrote:

I always thought it would be neat to be able to get the binding at the
time an exception was raised so that one could see the state the stack
was in when an exception occurred.

It would be nice list the local variables and values at the time of the
exception for instance. Can anyone think of some trickery to do this?
I think it would be of great use for debugging and whatnot, and
massive points for cool ruby hackery :wink:
聽聽.adam sanderson

No trickery, but you do have to have the cooperation of the code which
raises:

def foo
聽聽a = "value of a"
聽聽raise StandardError, binding
end

def bar
聽聽foo
rescue => e
聽聽return e.message
end

b = bar

p b

p eval("a", b)
p eval("local_variables", b)

__END__

output:

#<Binding:0xb7e31a5c>
"value of a"
["a"]

路路路

--
聽聽聽聽聽聽vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

(Robert) #3

Adam Sanderson wrote:

I always thought it would be neat to be able to get the binding at the
time an exception was raised so that one could see the state the stack
was in when an exception occurred.

Note, that you can view the stack trace already with standard means:

10:11:06 [c]: ruby -e 'def f() raise "error" end; begin;f;rescue Exception
=> e; puts e.backtrace; end'
-e:1:in `f'
-e:1

It would be nice list the local variables and values at the time of
the exception for instance. Can anyone think of some trickery to do
this? I think it would be of great use for debugging and whatnot, and
massive points for cool ruby hackery :wink:
聽聽.adam sanderson

See Joel's solution.

Kind regards

聽聽聽聽robert

(Lothar Scholz) #4

Hello Adam,

I always thought it would be neat to be able to get the binding at the
time an exception was raised so that one could see the state the stack
was in when an exception occurred.

It would be nice list the local variables and values at the time of the
exception for instance. Can anyone think of some trickery to do this?
I think it would be of great use for debugging and whatnot, and
massive points for cool ruby hackery :wink:
聽聽.adam sanderson

Yes indeed and it is not possible with standart ruby at the moment.

But if you want it, then download ArachnoRuby from http://www.ruby-ide.com.

It just does what you want and it also automatically starts a post mortem
debugger when an unhandled exception terminats the program. So it is
easy to inspect the values.

路路路

--
Best regards, emailto: scholz at scriptolutions dot com
Lothar Scholz http://www.ruby-ide.com
CTO Scriptolutions Ruby, PHP, Python IDE 's

(Joel VanderWerf) #5

Joel VanderWerf wrote:

No trickery, but you do have to have the cooperation of the code which
raises:

And just for fun, because it's late here...

def foo
聽聽a = "value of a"
聽聽cc = nil
聽聽callcc{|cc|}
聽聽if a
聽聽聽聽raise StandardError, binding
聽聽else
聽聽聽聽return "normally"
聽聽end
end

def bar
聽聽foo
rescue => e
聽聽return e.message
end

b = bar

case b
when Binding
聽聽eval("a=nil", b)
聽聽eval("cc", b).call
else
聽聽puts "Exiting #{b}"
end

路路路

--
聽聽聽聽聽聽vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

(Adam Sanderson) #6

Well that's the trick of course :wink: Being able to view the variables
without explicity entering the binding when you create the exception.

I tried to think of some way to overide raise in the Kernel and add
some functionality to Exception, but didn't really get anywhere.
聽聽.adam