# Rubyw, RuntimeError, capturing the stack, FXRuby

**URL:** <https://www.ruby-forum.com/t/rubyw-runtimeerror-capturing-the-stack-fxruby/192351>\
**Category:** Ruby\
**Created:** [July 28, 2010, 10:45pm UTC](https://www.ruby-forum.com/t/rubyw-runtimeerror-capturing-the-stack-fxruby/192351 "2010-07-28T22:45:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [July 28, 2010, 10:45pm UTC](https://www.ruby-forum.com/t/rubyw-runtimeerror-capturing-the-stack-fxruby/192351/1 "2010-07-28T22:45:22Z")

</div>

Let’s say I’m running rubyw and a RunTimeError is raised …

and I’m using FXRuby (which is tangential to this question) …

and I want to put up a errorbox …

But I want to capture the error message that would normally go to  
stdout, i.e. the call stack …

How would I capture the call stack to a string in a raised exception?

* * *

begin  
raise RuntimeError, “Something bad happened”  
rescue

# What do I do here?

end

* * *

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [July 28, 2010, 10:58pm UTC](https://www.ruby-forum.com/t/rubyw-runtimeerror-capturing-the-stack-fxruby/192351/2 "2010-07-28T22:58:50Z")

</div>

begin  
puts 1/0  
rescue Exception, NameError =\> e  
print “this is an error [#{e}]”  
print $!.inspect #Ruby places a reference to the associated Exception  
object into the global variable $!  
end

output: this is an error [divided by 0]#\<ZeroDivisionError: divided by  
0\>

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [July 28, 2010, 11:09pm UTC](https://www.ruby-forum.com/t/rubyw-runtimeerror-capturing-the-stack-fxruby/192351/3 "2010-07-28T23:09:57Z")

</div>

Joseph,

This does not give the call stack with file names and line numbers of  
the exception.

Thanks.

Ralph

Wednesday, July 28, 2010, 2:58:20 PM, you wrote:

JES\> begin  
JES\> puts 1/0  
JES\> rescue Exception, NameError =\> e  
JES\> print “this is an error [#{e}]”  
JES\> print $!.inspect #Ruby places a reference to the associated  
Exception  
JES\> object into the global variable $!  
JES\> end

JES\> output: this is an error [divided by 0]#\<ZeroDivisionError: divided  
by 0\>

> > From: Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > Organization: Ralph S.  
> > Reply-To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > Date: Thu, 29 Jul 2010 05:44:36 +0900  
> > To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > Subject: rubyw, RuntimeError, capturing the stack, FXRuby

> > Let’s say I’m running rubyw and a RunTimeError is raised …

> > and I’m using FXRuby (which is tangential to this question) …

> > and I want to put up a errorbox …

> > But I want to capture the error message that would normally go to stdout, i.e.  
> > the call stack …

> > How would I capture the call stack to a string in a raised exception?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [July 28, 2010, 11:37pm UTC](https://www.ruby-forum.com/t/rubyw-runtimeerror-capturing-the-stack-fxruby/192351/4 "2010-07-28T23:37:42Z")

</div>

Ralph S. wrote:

> This does not give the call stack with file names and line numbers of  
> the exception.

Use Exception#backtrace:

irb(main):001:0\> begin  
irb(main):002:1\* raise “Error”  
irb(main):003:1\> rescue =\> e  
irb(main):004:1\> puts e.backtrace  
irb(main):005:1\> end  
(irb):2:in `irb_binding' /opt/rubies/ruby-1.9.1-p429/lib/ruby/1.9.1/irb/workspace.rb:80:in`eval’  
/opt/rubies/ruby-1.9.1-p429/lib/ruby/1.9.1/irb/workspace.rb:80:in  
`evaluate' /opt/rubies/ruby-1.9.1-p429/lib/ruby/1.9.1/irb/context.rb:216:in`evaluate’  
/opt/rubies/ruby-1.9.1-p429/lib/ruby/1.9.1/irb.rb:157:in `block (2 levels) in eval_input' /opt/rubies/ruby-1.9.1-p429/lib/ruby/1.9.1/irb.rb:271:in`signal\_status’  
/opt/rubies/ruby-1.9.1-p429/lib/ruby/1.9.1/irb.rb:154:in `block in eval_input' /opt/rubies/ruby-1.9.1-p429/lib/ruby/1.9.1/irb/ruby-lex.rb:244:in`block  
(2 levels) in each\_top\_level\_statement’  
/opt/rubies/ruby-1.9.1-p429/lib/ruby/1.9.1/irb/ruby-lex.rb:230:in `loop' /opt/rubies/ruby-1.9.1-p429/lib/ruby/1.9.1/irb/ruby-lex.rb:230:in`block  
in each\_top\_level\_statement’  
/opt/rubies/ruby-1.9.1-p429/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in  
`catch' /opt/rubies/ruby-1.9.1-p429/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in`each\_top\_level\_statement’  
/opt/rubies/ruby-1.9.1-p429/lib/ruby/1.9.1/irb.rb:153:in `eval_input' /opt/rubies/ruby-1.9.1-p429/lib/ruby/1.9.1/irb.rb:70:in`block in start’  
/opt/rubies/ruby-1.9.1-p429/lib/ruby/1.9.1/irb.rb:69:in `catch' /opt/rubies/ruby-1.9.1-p429/lib/ruby/1.9.1/irb.rb:69:in`start’  
/home/marvin/Programmieren/Programme/irb\_/irb\_.rb:37:in `’  
=\> nil  
irb(main):006:0\>

Vale,  
Marvin

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [July 28, 2010, 11:59pm UTC](https://www.ruby-forum.com/t/rubyw-runtimeerror-capturing-the-stack-fxruby/192351/5 "2010-07-28T23:59:43Z")

</div>

Sorry…

Des this help?

begin  
puts 1/0  
rescue Exception, NameError =\> e  
print “this is an error [#{e}] -----\>[#{e.backtrace}]\<-------”  
print $!.inspect #Ruby places a reference to the associated Exception  
object into the global variable $!  
end

this is an error [divided by 0]  
-----\>[/Users/jes/error.rb:2:in `/’/Users/jes/error.rb:2]\<-------  
#\<ZeroDivisionError: divided by 0\>
