Project

General

Profile

Actions

Misc #19054

closed

`else` in exception-handling context vs early return

Added by zverok (Victor Shepelev) over 1 year ago. Updated 7 months ago.

Status:
Closed
Assignee:
-
[ruby-core:110284]

Description

else in exception-handling context is rarely used (at least in codebase I saw), so we encountered it just recently:

def foo
  puts "body"
  return
rescue => e
  p e
else
  puts "else"
ensure
  puts "ensure"
end

foo # prints "body", then "ensure"

[1].each do
  puts "body"
  next
rescue => e
  p e
else
  puts "else"
ensure
  puts "ensure"
end
# also prints "body" then "ensure"

E.g. else is ignored in both cases. Intuitively, I would expect that if no exception is raised in block, else is performed always—like ensure is performed always, exception or not, early return or not.

I found only a very old discussion of this behavior in #4473 (it was broken accidentally on the road to 1.9.2, but then fixed back), but it doesn't explain the reason for it.

Can somebody provide an insight on this decision, and whether it is justified at all?.. At least, it should be documented somewhere, exception handling docs doesn't mention this quirk.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0