> > why doesn't "CCCC" get printed?
> > [loaderr.rb]
> > ----
> > 1/0 rescue ZeroDivisionError
> > puts "AAAA"
> > begin ; require "nonexisting" ; rescue LoadError ; end
> > puts "BBBB"
> > require "nonexisting" rescue LoadError
> > puts "CCCC"
1. Is there really no official reference where I can look
this up? Where can I find this fact in the source code?
3. Why doesn't Ruby complain or at least warn when I give a
non-`StandardError` exception after the `rescue` modifier?
I think you have misunderstood the meaning of the value after the rescue
As others already told you, the rescue modifier always rescues exceptions
derived from StandardError. To be more explicit, this doesn't only mean you
can't rescue exceptions not derived from StandardError, it also means you
can't choose which exceptions are rescued: for example, you can't use the
rescue modifier to rescue a ZeroDivisionError but not an ArgumentError. For
1/0 rescue ArgumentError
won't raise any exception.
To find out the meaning of the statement after the rescue modifier, you can run
the line above in irb:
irb(main):001:0> 1/0 rescue ArgumentError
This shows you that the value returned by the the whole expression is the
result of the statement after the rescue modifier. Another example:
irb(main):003:0> 1/0 rescue 27
irb(main):007:0> "hello"[:a] rescue "x"
In short, the meaning of the rescue modifier is the following:
- attempt to execute the statement before "rescue"
- if an exception derived from StandardError is raised, execute the statement
After all of this, you can see that the answer to your question, why doesn't
ruby complain when i write 1/0 rescue LoadError?, is that it doesn't complain
because what you wrote is perfectly correct. It means:
- execute 1/0
- if it raises an exception, use the class LoadErorr as the result of the
irb(main):008:0> x = 1/0 rescue LoadError
As for your first question, I looked this up in the book "The ruby programming
language" by Flanagan and Matsumoto, but it's quite old (it's been published
in 2008 and only covers ruby 1.8 and ruby 1.9). I don't know whether there are
more recent references.
I hope this helps
On martedì 2 giugno 2020 07:55:36 CEST Bertram Scharpf wrote:
On Monday, 01. Jun 2020, 08:28:58 -0300, Clavius Tales wrote:
> On Mon, Jun 1, 2020 at 8:12 AM Bertram Scharpf <email@example.com> wrote: