Is there a way to tell ruby that it must never skip levels in the
backtrace like this:
/lib/ruby/1.8/net/ftp.rb:192:in readline': End of file reached (EOFError) from /lib/ruby/1.8/net/ftp.rb:192:ingetline’
from /lib/ruby/1.8/net/ftp.rb:202:in getmultiline' from /lib/ruby/1.8/net/ftp.rb:216:ingetresp’
from /lib/ruby/1.8/net/ftp.rb:232:in voidresp' from /lib/ruby/1.8/net/ftp.rb:157:inconnect’
from /lib/ruby/1.8/net/ftp.rb:155:in synchronize' from /lib/ruby/1.8/net/ftp.rb:158:inconnect’
from /lib/ruby/1.8/net/ftp.rb:119:in initialize' ... 7 levels... from getp.rb:85:inget_from’
from getp.rb:85:in open' from getp.rb:85:inget_from’
from getp.rb:120
This is the backtrace the ruby interpreter prints out when it terminates
due to an unhandled exception.
See http://www.ruby-talk.org/75008 and following. Basically it comes down
to, you have to wrap your code in an exception handler if you want all the
information, like so:
require ‘pp’
begin
… all your code …
rescue Exception => e
pp e.message
pp e.class
pp e.backtrace
end
I’ve used pp here, because it should give a much more readable printout of
the backtrace with minimal work.
Is there a way to tell ruby that it must never skip levels in the
backtrace like this:
/lib/ruby/1.8/net/ftp.rb:192:in readline': End of file reached (EOFError) from /lib/ruby/1.8/net/ftp.rb:192:in getline’
from /lib/ruby/1.8/net/ftp.rb:202:in getmultiline' from /lib/ruby/1.8/net/ftp.rb:216:in getresp’
from /lib/ruby/1.8/net/ftp.rb:232:in voidresp' from /lib/ruby/1.8/net/ftp.rb:157:in connect’
from /lib/ruby/1.8/net/ftp.rb:155:in synchronize' from /lib/ruby/1.8/net/ftp.rb:158:in connect’
from /lib/ruby/1.8/net/ftp.rb:119:in initialize' ... 7 levels... from getp.rb:85:in get_from’
from getp.rb:85:in open' from getp.rb:85:in get_from’
from getp.rb:120
This is the backtrace the ruby interpreter prints out when it
terminates due to an unhandled exception.
What I was really hoping for a is a command line switch to the ruby
interpreter, i.e. something that I do not need to modify my source for.
Maybe I can require some file on the command line with
-r complete_backtrace that implements $!.backtrace printing
in an atexit handler? Otherwise, I plan to modify my ruby interpreter to
make the maximum number of stack frames printed configurable via
environment variable.
If you catch the exception (‘err’) and ‘puts err.backtrace’, you’ll
get the full story in the following form:
…
e.rb:7:in b' e.rb:3:in a’
e.rb:7:in b' e.rb:3:in a’
e.rb:7:in `b’
…
If you do a more complicated printing routine as Matz suggested, you
get this:
...
from e.rb:7:in `b'
from e.rb:3:in `a'
from e.rb:7:in `b'
from e.rb:3:in `a'
from e.rb:7:in `b'
from e.rb:3:in `a'
...
For information purposes, I’d just ‘puts err.backtrace’ and be done
with it. Note in both cases, the full trace is printed, not “…”!
Gavin
···
On Wednesday, November 26, 2003, 7:47:10 PM, Tobias wrote:
Is there a way to tell ruby that it must never skip levels in the
backtrace like this:
/lib/ruby/1.8/net/ftp.rb:192:in readline': End of file reached (EOFError) from /lib/ruby/1.8/net/ftp.rb:192:in getline’
from /lib/ruby/1.8/net/ftp.rb:202:in getmultiline' from /lib/ruby/1.8/net/ftp.rb:216:in getresp’
from /lib/ruby/1.8/net/ftp.rb:232:in voidresp' from /lib/ruby/1.8/net/ftp.rb:157:in connect’
from /lib/ruby/1.8/net/ftp.rb:155:in synchronize' from /lib/ruby/1.8/net/ftp.rb:158:in connect’
from /lib/ruby/1.8/net/ftp.rb:119:in initialize' ... 7 levels... from getp.rb:85:in get_from’
from getp.rb:85:in open' from getp.rb:85:in get_from’
from getp.rb:120
I, for one, won’t remember this when I need it, so may I suggest
that this be wrapped for convenience in a standard method, so we can
do
begin
foo
rescue => e
e.full_backtrace # or some other name
exit 1
end
in some near-future ruby, please?
Thank you,
Hugh
···
On Wed, 26 Nov 2003, Yukihiro Matsumoto wrote:
Is there a way to tell ruby that it must never skip levels in the
backtrace like this:
begin
foo
rescue => e
cb = e.backtrace
print cb.shift, “:”, e.message, “\n”
cb.each{|c| print "\tfrom ", c, “\n”}
exit 1
end
Is there a way to tell ruby that it must never skip levels in the
You might want to enclose your ‘main’ into begin/rescue/end and print
the stacktrace for yourself
begin
main # main raises an exception, e.g. 1/0
rescue
caller.each{|x| p x}
end
I’m afraid, that won’t output the interesting part, namely the stack
levels below this method that make it possible to find the source of the
exception.
What I was really hoping for a is a command line switch to the ruby
interpreter, i.e. something that I do not need to modify my source for.
Maybe I can require some file on the command line with
-r complete_backtrace that implements $!.backtrace printing
in an atexit handler? Otherwise, I plan to modify my ruby interpreter to
make the maximum number of stack frames printed configurable via
environment variable.
Before you start, maybe Matz could commment on the idea of having a
~/.rubyrc that controls interpreter bahavior (of course together with a
switch that allows for ignoring this file). Then maybe he / we / you
implement this feature for the complete runtime and all / several command
line switches.
In message “Re: Backtrace without skips needed” on 03/11/27, “Robert Klemme” bob.news@gmx.net writes:
Before you start, maybe Matz could commment on the idea of having a
~/.rubyrc that controls interpreter bahavior (of course together with a
switch that allows for ignoring this file). Then maybe he / we / you
implement this feature for the complete runtime and all / several command
line switches.
I’d say “no” to the rc idea. I believe it’s application matter, not
language matter, i.e. there can be no common configuration among all
applications written in Ruby.
“Yukihiro Matsumoto” matz@ruby-lang.org schrieb im Newsbeitrag
news:1069888395.324444.21319.nullmailer@picachu.netlab.jp…
Hi,
Before you start, maybe Matz could commment on the idea of having a
~/.rubyrc that controls interpreter bahavior (of course together with a
switch that allows for ignoring this file). Then maybe he / we / you
implement this feature for the complete runtime and all / several
command
line switches.
I’d say “no” to the rc idea. I believe it’s application matter, not
language matter, i.e. there can be no common configuration among all
applications written in Ruby.
Sounds reasonable considering that a change to the rc file could have
suprising side effects on other applications. Thanks for pointing my nose
on that!
Regards
robert
···
In message “Re: Backtrace without skips needed” > on 03/11/27, “Robert Klemme” bob.news@gmx.net writes: