Question

When I call Exception#backtrace_locations, it usually returns an array, as intended:

begin
  raise "foo"
rescue => e
  p e.backtrace_locations
end
# => ["this_file:2:in `<main>'"]

This is the same if I raise an ArgumentError manually:

begin
  raise ArgumentError.new
rescue => e
  p e.backtrace_locations
end
# => ["this_file:2:in `<main>'"]

However, when I raise a real ArgumentError by calling a method with wrong number of arguments, the backtrace_locations is nil, which is unexpected to me:

def foo; end

begin
  foo(:bar)
rescue => e
  p e.backtrace_locations
end
# => nil

Under the same situation, the classic Exception#backtrace returns an array, as intended:

def foo; end

begin
  foo(:bar)
rescue => e
  p e.backtrace
end
# => ["this_file:1:in `foo'", "this_file:4:in `<main>'"]

Is the return value of Exception#backtrace_locations being nil in the third case above intended? If so, when does Exception#backtrace_locations become nil? Is there any documentation for this? Or, is it a Ruby bug?

At this point, I think it is a bug, and reported it.

Was it helpful?

Solution

It was a bug, and the maintainer ko1 just fixed it in Revision 44411. Hopefully, it will make it into the release of Ruby 2.1 today.

Edit Turns out that it has not been fixed yet. Ruby 2.1 released today still has this issue.

Edit According to a maintainer, the fix will be incorporated into Ruby 2.1.1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top