Where's the backtrace?

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

lists wrote:

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

Check out the documentation for the Exception class:

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

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
Exception#backtrace: class Exception - RDoc Documentation

···

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

$ 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.

That's what I was missing, thanks!

-Ryan

···

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

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