user1934428
user1934428

Reputation: 22356

Combining `raise` and `throw`

I have a function:

def g
  ''.each_line.next
end

Since each_line returns a line iterator over an empty string, next raises an exception StopIteration: iteration reached an end.

On top of this, I have another function:

def f
  catch :aSymbol do
    loop do
      g
    end
  end
end

catch will catch the symbol :aSymbol if it is thrown somewhere, which is not the case, so this function should raise the same exception. However, calling f doesn't do so; it behaves, as if the catch would silently rescue the exception too. Is this the expected behaviour?

Upvotes: 1

Views: 58

Answers (1)

Frederick Cheung
Frederick Cheung

Reputation: 84182

This is nothing to do with catch - the behaviour can be observed with

def f
  loop do # Loop will abort during first iteration
    g
  end
end

This is because loop rescues StopIteration and interprets that to mean that you want to break out of loop (see definition of loop)

Upvotes: 5

Related Questions