B Seven
B Seven

Reputation: 45943

Is it possible to get the line number that threw an error?

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

Answers (1)

klump
klump

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

Related Questions