当我调用时Exception#backtrace_locations
,它通常会按预期返回一个数组:
begin
raise "foo"
rescue => e
p e.backtrace_locations
end
# => ["this_file:2:in `<main>'"]
如果我ArgumentError
手动提出一个,这也是一样的:
begin
raise ArgumentError.new
rescue => e
p e.backtrace_locations
end
# => ["this_file:2:in `<main>'"]
ArgumentError
但是,当我通过调用具有错误数量参数的方法来引发 real 时, backtrace_locations
is nil
,这对我来说是出乎意料的:
def foo; end
begin
foo(:bar)
rescue => e
p e.backtrace_locations
end
# => nil
在相同的情况下,经典Exception#backtrace
返回一个数组,如预期的那样:
def foo; end
begin
foo(:bar)
rescue => e
p e.backtrace
end
# => ["this_file:1:in `foo'", "this_file:4:in `<main>'"]
上述第三种情况的返回值是Exception#backtrace_locations
有意nil
的吗?如果是这样,什么时候Exception#backtrace_locations
成为nil
?有这方面的文件吗?或者,它是一个 Ruby 错误?
在这一点上,我认为这是一个错误,并报告了它。