From: "Eregon (Benoit Daloze)" Date: 2021-10-21T10:24:33+00:00 Subject: [ruby-core:105727] [Ruby master Feature#18083] Capture error in ensure block. Issue #18083 has been updated by Eregon (Benoit Daloze). ko1 (Koichi Sasada) wrote in #note-20: > Sorry, my proposal is introducing dynamic scope, not syntactical scope like: Ah, I misunderstood. Then I think deprecating `$!` is the right way, since that seems the only way to be able to analyze decently if an exception's backtrace is needed and do the important optimizations I mentioned in earlier comments. This would help a lot for making Ruby exceptions faster. I'm pretty sure @headius would agree since he did a similar optimization, but currently quite limited because of `$!` being accessible inside calls. And of course it would avoid the problem of `$!` having confusing semantics, by telling users to use `rescue => exc` instead which is clear, simple and clean. If needed maybe we can avoid warning for `rescue $!` since that's fairly harmless, but probably that's very rarely used in library/app code (i.e., only used in REPL and for trying things). --- We could also warn whenever `$!` is not used syntactically in the `rescue` clause, but IMHO better deprecate it entirely. ---------------------------------------- Feature #18083: Capture error in ensure block. https://bugs.ruby-lang.org/issues/18083#change-94226 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal ---------------------------------------- As discussed in https://bugs.ruby-lang.org/issues/15567 there are some tricky edge cases. As a general model, something like the following would be incredibly useful: ``` ruby begin ... ensure => error pp "error occurred" if error end ``` Currently you can get similar behaviour like this: ``` ruby begin ... rescue Exception => error raise ensure pp "error occurred" if error end ``` The limitation of this approach is it only works if you don't need any other `rescue` clause. Otherwise, it may not work as expected or require extra care. Also, Rubocop will complain about it. Using `$!` can be buggy if you call some method from `rescue` or `ensure` clause, since it would be set already. It was discussed extensively in https://bugs.ruby-lang.org/issues/15567 if you want more details. -- https://bugs.ruby-lang.org/ Unsubscribe: