Reputation: 6409
How do I make the exception reraised from bar
in the last line look like it came from there, and not from the block? I want to see bar
in the backtrace.
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)
Edit:
The current backtrace is
test.rb:2:in `<main>': foo (RuntimeError)
what I want is (or sth similar)
test.rb:10:in `bar': foo (RuntimeError)
from test.rb:13:in `<main>'
Upvotes: 4
Views: 678
Reputation: 44725
I am not sure if that's what you want, but you can try:
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
Upvotes: 1
Reputation: 11274
I am not sure if this is the correct answer. But I decided to give it a go :-)
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>'
Second try
begin
...
end
sleep 1
def bar
$e.set_backtrace(["bar"])
raise $e
end
bar #=> bar: foo (RuntimeError)
Upvotes: 1