开发者

Outputting errors in a rescue (Ruby/Rails)

Just a quick question. I cant find it in the documentation.

If I use a standard begin ... rescue, how do I print all the errors or st开发者_高级运维ack trace into the rescue?

e.g.:

begin 
    do x
rescue
    puts errors
end

Any ideas?


There are at least two ways that I know of to get the error. The first is using a global variable: $! which is always set to the last error that occurred. The second is by explicitly capturing the error when you rescue:

begin
  # do something that fails...
rescue => error
  # error and $! are equivalent here
end

Either one will let you inspect or print out the backtrace using either:

$!.backtrace # => array of backtrace steps
error.backtrace # => same error
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜