require

Hi

How can I ignore a require - error?

thank you
Opti

Hi

How can I ignore a require - error?

Yes, it raises an exception that you can rescue.

···

On Fri, Apr 22, 2022 at 10:40 AM Die Optimisten <inform@die-optimisten.net> wrote:

#!/usr/bin/env ruby
# encoding: utf-8

begin
  require 'notfound'
rescue LoadError => e
  $stderr.puts "#{e} not found"
end

puts "We continue nevertheless”

···

El 22 abr. 2022, a las 05:39, Die Optimisten <inform@die-optimisten.net> escribió:

Hi

How can I ignore a require - error?


Gonzalo Garramuno
ggarra13@gmail.com

Unlike many errors, `LoadError` cannot be rescued inline, either, as it
does not derive from `StandardError`, so it must be rescued in a block.

···

On Fri, Apr 22, 2022 at 5:07 AM Gonzalo Garramuno <ggarra13@gmail.com> wrote:

> El 22 abr. 2022, a las 05:39, Die Optimisten <inform@die-optimisten.net> > escribió:
>
> Hi
>
> How can I ignore a require - error?

#!/usr/bin/env ruby
# encoding: utf-8

begin
  require 'notfound'
rescue LoadError => e
  $stderr.puts "#{e} not found"
end

puts "We continue nevertheless”


Gonzalo Garramuno
ggarra13@gmail.com

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<ruby-talk list: member options login page>

--
Austin Ziegler • halostatue@gmail.com • austin@halostatue.ca
http://www.halostatue.ca/http://twitter.com/halostatue

Can you give an example for inline- and block-rescue?

I thought rescue Error.... # where Error is just a 'filter' ?

Sp rescue # without named Errors just "filters" StandardErrors ?

Opti

···

Am 22.04.22 um 19:04 schrieb Austin Ziegler:

Unlike many errors, `LoadError` cannot be rescued inline, either, as
it does not derive from `StandardError`, so it must be rescued in a block.

Some people say not to use it at all. Those people are wrong, full stop.

-a

···

On Fri, Apr 22, 2022 at 4:14 PM Die Optimisten <inform@die-optimisten.net> wrote:

Am 22.04.22 um 19:04 schrieb Austin Ziegler:
> Unlike many errors, `LoadError` cannot be rescued inline, either, as
> it does not derive from `StandardError`, so it must be rescued in a
block.

Can you give an example for inline- and block-rescue?

I thought rescue Error.... # where Error is just a 'filter' ?

Sp rescue # without named Errors just "filters" StandardErrors ?

Opti

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<ruby-talk list: member options login page>

--
Austin Ziegler • halostatue@gmail.com • austin@halostatue.ca
http://www.halostatue.ca/http://twitter.com/halostatue

Hi
Why is doing
begin require 'badfile'; rescue; end
(alone) NOT doing the job (as expected)?
Thought, that should catch alllllll ?!?
Is this explainable, do we find this behaviour for other exceptions too?
Opti

Because, as mentioned already, `LoadError` does not inherit from `StandardError`:

LoadError.ancestors
=> [LoadError, ScriptError, Exception, Object, PP::ObjectMixin, Kernel, BasicObject]

If you do not supply an argument to `rescue`, then it only catches instances of `StandardError`.

This works:

begin require 'badfile'; rescue LoadError; end

But it’s not really an “inline rescue” — it’s using semi-colons!

···

On 27 Apr 2022, at 10:54, Die Optimisten <inform@die-optimisten.net> wrote:

Hi
Why is doing
   begin require 'badfile'; rescue; end
(alone) NOT doing the job (as expected)?
Thought, that should catch alllllll ?!?
Is this explainable, do we find this behaviour for other exceptions too?
Opti

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<ruby-talk list: member options login page>

Whats 'inline' (and not inline) rescue?
I only know one type of rescue?
thank you
Opti

Inline rescue is a rescue of the form of:

x = object.do_something rescue other_value

Which is an equivalent of:

x = begin
object.do_something
rescue
other_value
end

···

On 4/27/22 13:14, Die Optimisten wrote:

Whats 'inline' (and not inline) rescue?
I only know one type of rescue?
thank you
Opti