Does the Ruby rescue statement work with require?

一个人想着一个人 提交于 2019-12-22 08:48:33

问题


Does the Ruby rescue statement modifier work with require?

irb(main):001:0> require 'a' rescue nil
LoadError: no such file to load -- a
    from (irb):1:in `require'
    from (irb):1
    from :0

回答1:


You can rescue from a LoadError you just need to use the begin/end style and not use the inline rescue:

This works as you expect:

begin
 require 'a'
rescue LoadError => ex
 puts "Load error: #{ex.message}"
end


来源:https://stackoverflow.com/questions/12750546/does-the-ruby-rescue-statement-work-with-require

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!