【问题标题】:Is it possible to get the line number that threw an error?是否可以获得引发错误的行号?
【发布时间】:2012-04-20 12:05:54
【问题描述】:
begin
  . . .
  # error occurs here
  . . .
rescue => error
  puts "Error: " + error.message
end

有没有办法获取发生错误的语句的行号

【问题讨论】:

    标签: ruby exception exception-handling


    【解决方案1】:

    只需回溯:

    begin
      . . .
      # error occurs here
      . . .
    rescue => error
      puts "Error: " + error.message
      puts error.backtrace
    end
    

    仅获取行号 - 只需通过正则表达式将其从回溯中解析出来。

    更多信息可以在这里找到:Catching line numbers in ruby exceptions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      • 2013-01-06
      相关资源
      最近更新 更多