From: "jeremyevans0 (Jeremy Evans)" Date: 2022-10-14T16:32:40+00:00 Subject: [ruby-core:110288] [Ruby master Misc#19054] `else` in exception-handling context vs early return Issue #19054 has been updated by jeremyevans0 (Jeremy Evans). zverok (Victor Shepelev) wrote in #note-2: > ...e.g. NOTHING is achieved by `else` that can't be achieved by putting exactly the same code at the end of the body. Incorrect. The primary reason for `else` with `rescue` is that code in `else` that raises exceptions does not call into the `rescue` blocks (though it is still handled by `ensure`). Code that the end of the body obviously would. In any case, I would guess there is no chance of changing the behavior at this point. It would break way too much code. ---------------------------------------- Misc #19054: `else` in exception-handling context vs early return https://bugs.ruby-lang.org/issues/19054#change-99573 * Author: zverok (Victor Shepelev) * Status: Open * Priority: Normal ---------------------------------------- `else` in exception-handling context is rarely used (at least in codebase I saw), so we encountered it just recently: ```ruby 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](https://docs.ruby-lang.org/en/master/syntax/exceptions_rdoc.html) doesn't mention this quirk. -- https://bugs.ruby-lang.org/ Unsubscribe: