Failed requires?

T

Terry Michaels

I'm running ruby 1.8.7. If I 'require' a file, and it fails, the program
just crashes with message like so:

no such file to load -- whatsit
from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require'
from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
from (irb):1

or (if if I turn auto_gem off):

/main.rb:3:in `require': no such file to load -- whatsit (LoadError)
from ./main.rb:3

It seems like I should be given the chance to do something else if a
require fails, such as print an more understandable message to the
console or put something in a log. But it seems that ruby does not
return from the 'require' call, or allow catching an exception. Am I
right? And why is this?
 
Q

Quintus

Am 30.01.2011 07:07, schrieb Terry Michaels:
I'm running ruby 1.8.7. If I 'require' a file, and it fails, the program
just crashes with message like so:

no such file to load -- whatsit
from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require'
from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
from (irb):1

or (if if I turn auto_gem off):

./main.rb:3:in `require': no such file to load -- whatsit (LoadError)
from ./main.rb:3

It seems like I should be given the chance to do something else if a
require fails, such as print an more understandable message to the
console or put something in a log. But it seems that ruby does not
return from the 'require' call, or allow catching an exception. Am I
right? And why is this?

begin
require "whatsit"
rescue LoadError => e
puts "Failed to load whatsit:"
puts e.message
puts "Please install it."
exit 1
end
 
N

none

Terry Michaels a écrit :
I'm running ruby 1.8.7. If I 'require' a file, and it fails, the program
just crashes with message like so:

no such file to load -- whatsit
from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require'
from /usr/lib64/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
from (irb):1

or (if if I turn auto_gem off):

/main.rb:3:in `require': no such file to load -- whatsit (LoadError)
from ./main.rb:3

It seems like I should be given the chance to do something else if a
require fails, such as print an more understandable message to the
console or put something in a log. But it seems that ruby does not
return from the 'require' call, or allow catching an exception. Am I
right? And why is this?

Works for me :

$ irb
irb(main):001:0> require 'foo'
LoadError: no such file to load -- foo
from (irb):1:in `require'
from (irb):1
irb(main):002:0> begin
irb(main):003:1* require 'foo'
irb(main):004:1> rescue LoadError => e
irb(main):005:1> puts 'do something else'
irb(main):006:1> end
do something else
=> nil
 
B

Brian Candler

Terry Michaels wrote in post #978462:
It seems like I should be given the chance to do something else if a
require fails, such as print an more understandable message to the
console or put something in a log. But it seems that ruby does not
return from the 'require' call, or allow catching an exception. Am I
right? And why is this?

A bare "rescue" without specifying a class will rescue StandardError
(and all its subclasses). But LoadError is not a subclass of
StandardError:

irb(main):001:0> LoadError.ancestors
=> [LoadError, ScriptError, Exception, Object, Kernel]

So you have to rescue it explicitly.

Google "ruby exception hierarchy" for more info.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,614
Members
45,288
Latest member
Top CryptoTwitterChannels

Latest Threads

Top