From: "Eregon (Benoit Daloze)" Date: 2021-08-20T22:09:32+00:00 Subject: [ruby-core:105035] [Ruby master Feature#18083] Capture error in ensure block. Issue #18083 has been updated by Eregon (Benoit Daloze). BTW, deprecating and removing `$!` has another advantage: a raised Ruby exception wouldn't immediately leak to the current thread/fiber and more optimizations might be possible, including skipping to compute the exception backtrace (the most expensive part of Ruby exceptions) if e.g. a JIT can prove the exception or its backtrace is never used. That analysis would only require tracking usages of `exc` in `rescue => exc`, and not any call in the `rescue` that might use `$!`. Of course, the related `$@` (which is `$?.backtrace`) should be deprecated and removed too. ---------------------------------------- Feature #18083: Capture error in ensure block. https://bugs.ruby-lang.org/issues/18083#change-93444 * 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: