Issue #15936 has been updated by duerst (Martin D=FCrst).


kylemacey (Kyle Macey) wrote:

> This is coming from a need that I personally face often on the utilities =
I work on, where I need to update state on an object if something unexpecte=
d happens. My company's linter gets upset when I use the `rescue StandardEr=
ror` pattern, so I was hoping to have a way to be more explicit that I'm no=
t trying to prevent the error from going up the stack, I just want to act u=
pon the exception.

What about getting the linter to recognize that you are using `raise` again=
 in the `rescue` clause? That shouldn't be too difficult, at least for the =
simple cases.


----------------------------------------
Feature #15936: on_error in lieu of rescue, raise
https://bugs.ruby-lang.org/issues/15936#change-78693

* Author: kylemacey (Kyle Macey)
* Status: Open
* Priority: Normal
* Assignee: =

* Target version: =

----------------------------------------
A common bad pattern in ruby is to rescue any exception and accidentally cl=
obber the exception. =


```
begin
  some_method
rescue StandardError
  # =

end
```

Most linters will complain if you write rescues like the code above. Howeve=
r, this could be useful if we want to perform an operation on _any_ error, =
as long as we re-raise the exception after doing our work.

```
begin
  some_method
rescue StandardError
  job.fail! =

  raise
end
```

Here, though, we run the risk of potentially forgetting to reraise the exce=
ption, or having to make exceptions in our linter for an operation that is =
overall benign.

What would be a thought on using another keyword that doesn't actually _res=
cue_ an exception, but performs an operation in the event of an error? Simi=
lar to `ensure`, but only in the event of an error.

```
begin
  some_method
on_error StandardError
  job.fail! =

end
```

(obviously, someone more creative than me should come up with a better name)



-- =

https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=3Dunsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>