# Where's the backtrace?

**URL:** <https://www.ruby-forum.com/t/wheres-the-backtrace/63875>\
**Category:** Ruby\
**Created:** [June 20, 2006, 1:21am UTC](https://www.ruby-forum.com/t/wheres-the-backtrace/63875 "2006-06-20T01:21:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/cdc98d/32.png) [@lists](https://www.ruby-forum.com/u/lists)\
**Post date:** [June 20, 2006, 1:21am UTC](https://www.ruby-forum.com/t/wheres-the-backtrace/63875/1 "2006-06-20T01:21:47Z")

</div>

I’m probably misunderstanding the way things are supposed to work.  
Here’s what I’m seeing:

$ cat lib.rb  
class MyClass  
def throws\_an\_exception  
self.non\_existent\_method  
end  
end

if **FILE** == $0  
MyClass.new.throws\_an\_exception  
end  
$ ruby lib.rb  
lib.rb:3:in `throws_an_exception': undefined method`non\_existent\_method’ for #MyClass:0x255bc (NoMethodError)  
from lib.rb:8

So far, this is what I expect. Now consider:

$ cat the\_test.rb  
require ‘./lib’

begin  
MyClass.new.throws\_an\_exception  
rescue =\> e  
puts e  
end  
$ ruby the\_test.rb  
undefined method `non\_existent\_method’ for #MyClass:0x2560c

I don’t get the rest of the backtrace (line number, etc.) unless I  
use the -d flag:

$ ruby -d the\_test.rb  
Exception `NoMethodError' at ./lib.rb:3 - undefined method`non\_existent\_method’ for #MyClass:0x2560c  
undefined method `non\_existent\_method’ for #MyClass:0x2560c

Is there a way to get the “full” backtrace when calling “the\_test.rb”  
without using the -d flag? I hope this makes sense.

Thanks,  
Ryan

---

<div class="post-metadata">

**Author:** ![lists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/cdc98d/32.png) [@lists](https://www.ruby-forum.com/u/lists)\
**Post date:** [June 20, 2006, 1:37am UTC](https://www.ruby-forum.com/t/wheres-the-backtrace/63875/2 "2006-06-20T01:37:03Z")

</div>

lists wrote:

> if **FILE** == $0  
> require ‘./lib’  
> the -d flag:  
> Ryan

Check out the documentation for the Exception class:

[http://ruby-doc.org/core/classes/Exception.html](http://ruby-doc.org/core/classes/Exception.html)

There is a method for retrieving the backtrace. Try this:

begin  
MyClass.new.throws\_an\_exception  
rescue =\> e  
puts e  
puts e.backtrace.join(“\n”)  
end

-Justin

---

<div class="post-metadata">

**Author:** ![lists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/cdc98d/32.png) [@lists](https://www.ruby-forum.com/u/lists)\
**Post date:** [June 20, 2006, 1:40am UTC](https://www.ruby-forum.com/t/wheres-the-backtrace/63875/3 "2006-06-20T01:40:10Z")

</div>

On Jun 20, 2006, at 0:18, lists wrote:

> I don’t get the rest of the backtrace (line number, etc.) unless I  
> use the -d flag:
> 
> $ ruby -d the\_test.rb  
> Exception `NoMethodError' at ./lib.rb:3 - undefined method `non\_existent\_method’ for #MyClass:0x2560c  
> undefined method `non\_existent\_method’ for #MyClass:0x2560c
> 
> Is there a way to get the “full” backtrace when calling  
> “the\_test.rb” without using the -d flag? I hope this makes sense.

The culprit is that you’re rescuing the exception, and simply  
printing its name. To get the whole backtrace, you could not rescue  
the exception in the first place, re-raise the exception, or print  
the backtrace manually.

matthew smillie.

begin  
MtClass.new.throws\_an\_exception  
rescue =\> e  
puts “oops, an exception”  
raise e  
end

begin  
MyClass.new.throws\_an\_exception  
rescue =\> e  
puts e  
puts e.backtrace  
end

Relevant docs are:  
Kernel#raise: [module Kernel - RDoc Documentation](http://ruby-doc.org/core/classes/Kernel.html#M002948)  
Exception#backtrace: [http://ruby-doc.org/core/classes/](http://ruby-doc.org/core/classes/)  
Exception.html#M001216

---

<div class="post-metadata">

**Author:** ![lists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/cdc98d/32.png) [@lists](https://www.ruby-forum.com/u/lists)\
**Post date:** [June 20, 2006, 2:10am UTC](https://www.ruby-forum.com/t/wheres-the-backtrace/63875/4 "2006-06-20T02:10:40Z")

</div>

On Jun 19, 2006, at 6:36 PM, Matthew S. wrote:

> puts e  
> puts e.backtrace  
> end

That’s what I was missing, thanks!

-Ryan
