From: "Eregon (Benoit Daloze)" Date: 2021-08-18T17:04:29+00:00 Subject: [ruby-core:104971] [Ruby master Feature#18083] Capture error in ensure block. Issue #18083 has been updated by Eregon (Benoit Daloze). The second snippet seems clear, the first one much less. In what cases `error` is set? If it's a StandardError? If it's an Exception? If it's any kind of "Ruby exception"? IMHO this pattern is rarely enough needed that the second snippet is good enough. If you need other `rescue`, you can add them in a nested begin/end, or in a different method, no? BTW I think the second snippet only differentiates `Exception` and put together regular execution and control flow exit, is that intended? ---------------------------------------- Feature #18083: Capture error in ensure block. https://bugs.ruby-lang.org/issues/18083#change-93336 * 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: