当我得到异常时,它通常来自调用堆栈的深处。当这种情况发生时,通常情况下,实际的违规代码行对我来说是隐藏的:
tmp.rb:7:in `t': undefined method `bar' for nil:NilClass (NoMethodError) from tmp.rb:10:in `s' from tmp.rb:13:in `r' from tmp.rb:16:in `q' from tmp.rb:19:in `p' from tmp.rb:22:in `o' from tmp.rb:25:in `n' from tmp.rb:28:in `m' from tmp.rb:31:in `l' ... 8 levels... from tmp.rb:58:in `c' from tmp.rb:61:in `b' from tmp.rb:64:in `a' from tmp.rb:67
那个“...... 8个级别......”截断给我带来了很多麻烦。我在谷歌上搜索这个没有太大的成功:我如何告诉 ruby 我希望转储包含完整的堆栈?
Exception#backtrace 包含整个堆栈:
def do_division_by_zero; 5 / 0; end begin do_division_by_zero rescue => exception puts exception.backtrace raise # always reraise end