You have full control over the backtrace returned by the exception instance using its method set_backtrace. For instance:
def strip_backtrace
yield
rescue => err
err.set_backtrace([])
raise err
end
begin
strip_backtrace do
puts 'hello'
raise 'ERROR!'
end
rescue => err
puts "Error message: #{err.message}"
puts "Error backtrace: #{err.backtrace}"
end
Conclusion:
hello
Error message: ERROR!
Error backtrace: []
The strip_backtrace method detects all errors, sets the backtrace to an empty array, and repeatedly raises the thrown exception.
source
share