Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Rescue To Display Full Backtrace

Tags:

Here's a real quick example:

puts File.join(nil, "hello") 

Would output

test.rb:4:in 'join': can't convert nil into String (TypeError) from test.rb:4 

But when I do this:

begin   puts File.join(nil, "hello") rescue => exception   puts exception.backtrace end 

This will output

test.rb:4:in 'join' test.rb:4 

Now how do I capture the full backtrace, including the "can't convert nil into String (TypeError)" part?

@Sarah Vessels: In my specific code, this snippet:

puts "==============================" puts error.message puts "==============================" puts error.inspect puts "==============================" puts error.backtrace puts "==============================" 

returns

============================== exit ============================== #<SystemExit: exit> ============================== /usr/lib/ruby/1.8/glib2.rb:37:in `exit' /usr/lib/ruby/1.8/glib2.rb:37:in `exit_application' multi.rb:234:in `main' multi.rb:347 ============================== 
like image 997
RyanScottLewis Avatar asked Sep 23 '09 15:09

RyanScottLewis


People also ask

What is backtrace Ruby?

This blog is part of our Ruby 2.5 series. Stack trace or backtrace is a sequential representation of the stack of method calls in a program which gets printed when an exception is raised.

What is rescue keyword in Ruby?

In Ruby, we use the rescue keyword for that. When rescuing an exception in Ruby, you can specify a specific error class that should be rescued from. begin raise 'This exception will be rescued!'

How do you raise errors in Ruby?

Ruby actually gives you the power to manually raise exceptions yourself by calling Kernel#raise. This allows you to choose what type of exception to raise and even set your own error message. If you do not specify what type of exception to raise, Ruby will default to RuntimeError (a subclass of StandardError ).


1 Answers

The value is stored there somewhere, based on this call to #inspect:

irb(main):001:0> begin irb(main):002:1* puts File.join(nil, "Hello") irb(main):003:1> rescue => exception irb(main):004:1> puts exception.inspect irb(main):005:1> end #<TypeError: can't convert nil into String> => nil 

Exception#message is the descriptive part:

irb(main):006:0> begin irb(main):007:1* puts File.join(nil, "hello") irb(main):008:1> rescue => ex irb(main):009:1> puts ex.message irb(main):010:1> end can't convert nil into String => nil 

So to get the type of data you're looking for, you could do something like the following:

irb(main):015:0> begin irb(main):016:1* puts File.join(nil, "hey") irb(main):017:1> rescue => ex irb(main):018:1> puts "#{ex.backtrace}: #{ex.message} (#{ex.class})" irb(main):019:1> end (irb):16:in `join'(irb):16:in `irb_binding'C:/Ruby/lib/ruby/1.8/irb/workspace.rb :52:in `irb_binding':0: can't convert nil into String (TypeError) => nil 
like image 124
Sarah Vessels Avatar answered Oct 26 '22 15:10

Sarah Vessels