Rescue modifier with require

Hi, all.

I’ve found that this works:

begin
require "file"
rescue
puts "oops"
end

but this doesn’t:

require “file” rescue puts “oops”

Should it? If not, why not?

What’s the general rule for when a
rescue modifier works “as expected”?

This is 1.7.3 on Windoze.

Hal

Hi,

but this doesn’t:

require “file” rescue puts “oops”

Should it? If not, why not?

rescue is a binary operator which takes two expressions, so you need
to code like:

require(“file”) rescue puts(“oops”)

Besides, require raises LoadError, which is not a subclass of
StandardError, that means it cannot be caught by rescue clause without
specifying exception class explicitly. rescue modifier is for casual
exception handling after all.

The syntax restriction can be relaxed. Let me see what I can.

						matz.
···

In message “rescue modifier with require” on 03/02/25, “Hal E. Fulton” hal9000@hypermetrics.com writes: