Does the Ruby rescue statement work with require?
Asked Answered
P

1

4

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
Primalia answered 5/10, 2012 at 16:41 Comment(0)
S
4

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
Spelter answered 5/10, 2012 at 16:48 Comment(1)
@RamondeCValle the rescue statement modifier only rescues StandardError (and its subclasses). LoadError isn’t a subclass of StandardError.Muttonchops

© 2022 - 2024 — McMap. All rights reserved.