rubyw, RuntimeError, capturing the stack, FXRuby

R

Ralph Shnelvar

[Note: parts of this message were removed to make it a legal post.]

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
- - -
 
R

Ralph Shnelvar

[Note: parts of this message were removed to make it a legal post.]

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>


 
M

Marvin Gülker

Ralph said:
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 `<main>'
=> nil
irb(main):006:0>

Vale,
Marvin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top