Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Exception#backtrace

exception.backtrace → array or nil

Returns any backtrace associated with the exception. The backtrace is an array of strings, each containing either “filename:lineNo: in ‘method”’ or “filename:lineNo.”

Example
def a
  raise "boom"
end

def b
  a()
end

begin
  b()
rescue => detail
  print detail.backtrace.join("\n")
end

produces:

prog.rb:2:in `a'
prog.rb:6:in `b'
prog.rb:10

In the case no backtrace has been set, nil is returned

Example
ex = StandardError.new
ex.backtrace
#=> nil
See also #as_json #backtrace_locations #== #cause also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1