How do I rescue from a `require': no such file to load in ruby? How do I rescue from a `require': no such file to load in ruby? ruby ruby

How do I rescue from a `require': no such file to load in ruby?


rescue without arguments rescues only StandardError s. The LoadError (that is raised by a file not found) is not a StandardError but a ScriptError (see http://blog.nicksieger.com/articles/2006/09/06/rubys-exception-hierarchy). Therefore you have to rescue the LoadError explicitly, as MBO indicated.


You have to explicitly define which error you want to rescue from.

begin  require 'someFile.rb'rescue LoadError  puts "someFile.rb was not found, have you"  puts "forgotten to specify the -I flag?"  exitend