[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Why must LoadError be explicitly caught from require

Ben Harper

8/30/2006 1:33:00 PM

Why do I explicitly have to catch LoadError as thrown by 'require'?

The following code demonstrates my problem:
------------------------------------------
begin
require 'not_existent'
rescue
print 'I am never reached'
#rescue LoadError
# print "But uncommenting these lines allows me to handle it"
end
------------------------------------------

I am running 1.8.4 on Windows.

--
Posted via http://www.ruby-....

4 Answers

Jano Svitok

8/30/2006 2:02:00 PM

0

On 8/30/06, Ben Harper <rogojin@gmail.com> wrote:
> Why do I explicitly have to catch LoadError as thrown by 'require'?
>
> The following code demonstrates my problem:
> ------------------------------------------
> begin
> require 'not_existent'
> rescue
> print 'I am never reached'
> #rescue LoadError
> # print "But uncommenting these lines allows me to handle it"
> end
> ------------------------------------------
>
> I am running 1.8.4 on Windows.

Because empty 'rescue' clause is the same as 'rescue StandardError',
and LoadError is not its subclass. See [1] for the hierarchy (or it's
in PickAxe as well).

This is documented in rdoc [2]; but don't worry, I overlooked it as well ;-)

[1] http://www.zenspider.com/Languages/Ruby/QuickR...
[2] http://ruby-doc.org/docs/ProgrammingRuby/html/tut_exceptio...:
...If you write a rescue clause with no parameter list, the parameter
defaults to StandardError.

khaines

8/30/2006 2:04:00 PM

0

khaines

8/30/2006 2:07:00 PM

0

Ben Harper

8/30/2006 2:53:00 PM

0

>
> Because LoadError is not a subclass of StandardError. A plain rescue
> catches StandardError and subclasses.
>
> Kirk Haines

Thanks!

--
Posted via http://www.ruby-....