Project

General

Profile

Actions

Feature #18083

open

Capture error in ensure block.

Added by ioquatix (Samuel Williams) over 2 years ago. Updated 4 months ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:104960]

Description

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:

begin
 ...
ensure => error
  pp "error occurred" if error
end

Currently you can get similar behaviour like this:

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.


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #15567: Allow ensure to match specific situationsRejectedioquatix (Samuel Williams)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0