如何在最后一行中从bar
重新加载的异常看起来像是从那里来的,而不是来自块?我希望在回溯中看到bar
。
begin
raise "foo"
rescue => e # yeah, i know
$e = e # oh boy, globals
end
sleep 1 # again, i know
def bar
raise $e
end
bar # => test.rb:2:in `<main>': foo (RuntimeError)
编辑:
目前的回溯是
test.rb:2:in `<main>': foo (RuntimeError)
我想要的是(或类似的)
test.rb:10:in `bar': foo (RuntimeError)
from test.rb:13:in `<main>'
答案 0 :(得分:1)
我不确定这是否是正确答案。但我决定试一试: - )
begin
raise "foo"
rescue => e
$e = e
end
sleep 1
def bar
raise $e.class, "bar"
end
bar #=> test.rb:10:in `bar': bar (RuntimeError)
from test.rb:13:in `<main>'
第二次尝试
begin
...
end
sleep 1
def bar
$e.set_backtrace(["bar"])
raise $e
end
bar #=> bar: foo (RuntimeError)
答案 1 :(得分:1)
我不确定这是不是你想要的,但你可以尝试:
begin
raise "foo"
rescue => e
$e = e
end
sleep 1
def get_full_stack
caller
end
def bar
exception = $e.dup
exception.set_backtrace get_full_stack
raise exception
end