B S*_*ven 10 ruby exception-handling exception
begin
. . .
# error occurs here
. . .
rescue => error
puts "Error: " + error.message
end
Run Code Online (Sandbox Code Playgroud)
有没有办法获取发生错误的语句的行号?
klu*_*ump 16
只需采取回溯:
begin
. . .
# error occurs here
. . .
rescue => error
puts "Error: " + error.message
puts error.backtrace
end
Run Code Online (Sandbox Code Playgroud)
仅获取行号 - 只需通过正则表达式将其从回溯中解析出来.
可在此处找到更多信息:捕获ruby异常中的行号