Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 507
B Seven Avatar asked Apr 07 '12 00:04

B Seven


1 Answers

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

like image 155
klump Avatar answered Nov 16 '22 04:11

klump