[ruby-core:115759] [Ruby master Feature#18083] Capture error in ensure block.

Issue #18083 has been updated by Dan0042 (Daniel DeLorme).
Giving `$!` dynamic scope mitigates the problem but still seems to encourage an approach that @matz has said he was against. Specifically, he was against `ensure => exception` and this is just a variant of that approach.
I believe Matz said he was against adding extra **syntax**, and that he was open to experimenting with ko1's proposal. Giving $! dynamic scope is indeed likely to cause subtle bugs, just less than currently. I think that almost all code currently written with `$!` in the ensure clause assume the exception is triggered within the same begin-end block. Using a dynamic scope would "magically" fix all of this currently-incorrect code. So it's a net win. And considering backwards compatibility I think it's unrealistic to consider deprecation. `$!` is used fairly often in gems. I also use it fairly often in my code, usually in some variants of the form `expr rescue $!` ---------------------------------------- Feature #18083: Capture error in ensure block. https://bugs.ruby-lang.org/issues/18083#change-105697 * 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/
participants (1)
-
Dan0042 (Daniel DeLorme)