Reputation: 45943
begin
. . .
# error occurs here
. . .
rescue => error
puts "Error: " + error.message
end
Is there a way to get the line number of the statement where the error occurred?
Upvotes: 11
Views: 4273
Reputation: 3269
Just take the backtrace:
begin
. . .
# error occurs here
. . .
rescue => error
puts "Error: " + error.message
puts error.backtrace
end
To get only the line number - just parse it out of the backtrace via a regex.
More information can be found here: Catching line numbers in ruby exceptions
Upvotes: 19